Skip to content

Instantly share code, notes, and snippets.

@msztolcman
Created June 29, 2011 21:16
Show Gist options
  • Save msztolcman/1055009 to your computer and use it in GitHub Desktop.
Save msztolcman/1055009 to your computer and use it in GitHub Desktop.
<?php
function p ($s) {
echo $s . "\n";
}
$str = 'a:b:c:d:e\:f\\\\:g';
function spl ($str, $delim, $escape='\\') {
if (strlen ($delim) != 1) {
return;
}
if (strlen ($str) == 0) {
return array ();
}
$ret = array ();
$idx = 0;
$idx_prev = 0;
$start_chunk = 0;
$is_delim = strlen ($delim);
while (1) {
if (($idx = strpos ($str, $delim, $idx_prev)) === false) {
$ret[] = substr ($str, $start_chunk);
break;
}
$chunk = substr ($str, $start_chunk, $idx - $start_chunk);
$esc_len = strlen ($chunk) - strlen (rtrim ($chunk, $escape));
if (!$is_delim || !$esc_len || ($esc_len % 2) == 0) {
$ret[] = $chunk;
$start_chunk = $idx + 1;
}
$idx_prev = $idx + 1;
}
return $ret;
}
$splitted = spl ($str, ':');
p ($str);
print_r($splitted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment