without ground rules, it's kinda hard to speculate but here's my version:
Code: Select all
<?php
// ...Ambush Commander's code was here
function splitSpacesQuotes($string)
{
$result = preg_split('#((?<!\\\\)(?:\\\\\\\\)*(["\']).*?(?<!\\\\)(?:\\\\\\\\)*\\2)|((?<=^|\s)(?!=["\'])(.+?)(?!=["\'])(?=\s|$))#s', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
$c = 0;
$final = array();
foreach($result as $k => $v)
{
if(!ctype_space($v) and (++$c % 2))
{
$final[] = $v;
}
}
return preg_replace('#^(["\'])(.*?)\\1$#s','\\2',$final);
}
$test[] = 'this is a "bunch of" words';
$test[] = 'this is a "test string" some escaped "quote \\" magic" along with handling "escapes \\\\" arf "arf';
$test[] = 'bang th\\\\is \\\\"Fo \\"o bar" \\\\\\"B"ang Bling" foo "Boo" foo';
$test[] = 'Extra \\s backslashes where they shouldn\'t be, plus an unterminated quote.';
$loop = 1000;
foreach($test as $t)
{
$amStart = microtime();
for($i = 0; $i < $loop; $i++)
{
$ambush = explodeSpacesQuotes($t);
}
$amStop = microtime();
$amStart = explode(' ',$amStart);
$amStop = explode(' ',$amStop);
$amTime = $amStop[0] - $amStart[0] + $amStop[1] - $amStart[1];
$fStart = microtime();
for($i = 0; $i < $loop; $i++)
{
$feyd = splitSpacesQuotes($t);
}
$fStop = microtime();
$fStart = explode(' ',$fStart);
$fStop = explode(' ',$fStop);
$fTime = $fStop[0] - $fStart[0] + $fStop[1] - $fStart[1];
echo '
Input:
'.$t.'
Ambush Commander: '.number_format($amTime,8).' seconds for '.$loop.' iterations ( '.number_format($amTime / $loop, .' second average )
'.var_export($ambush,true).'
feyd: '.number_format($fTime,8).' seconds for '.$loop.' iterations ( '.number_format($fTime / $loop, .' second average )
'.var_export($feyd,true).'
';
}
?>
outputs:
Code: Select all
Input:
this is a "bunch of" words
Ambush Commander: 0.13933206 seconds for 1000 iterations ( 0.00013933 second average )
array (
0 => 'this',
1 => 'is',
2 => 'a',
3 => 'bunch of',
4 => 'words',
)
feyd: 0.26041794 seconds for 1000 iterations ( 0.00026042 second average )
array (
0 => 'this',
1 => 'is',
2 => 'a',
3 => 'bunch of',
4 => 'words',
)
Input:
this is a "test string" some escaped "quote \" magic" along with handling "escapes \\" arf "arf
Ambush Commander: 0.44074011 seconds for 1000 iterations ( 0.00044074 second average )
array (
0 => 'this',
1 => 'is',
2 => 'a',
3 => 'test string',
4 => 'some',
5 => 'escaped',
6 => 'quote " magic',
7 => 'along',
8 => 'with',
9 => 'handling',
10 => 'escapes \\',
11 => 'arf"arf',
)
feyd: 0.73889494 seconds for 1000 iterations ( 0.00073889 second average )
array (
0 => 'this',
1 => 'is',
2 => 'a',
3 => 'test string',
4 => 'some',
5 => 'escaped',
6 => 'quote \\" magic',
7 => 'along',
8 => 'with',
9 => 'handling',
10 => 'escapes \\\\',
11 => 'arf',
12 => '"arf',
)
Input:
bang th\\is \\"Fo \"o bar" \\\"B"ang Bling" foo "Boo" foo
Ambush Commander: 0.41683507 seconds for 1000 iterations ( 0.00041684 second average )
array (
0 => 'bang',
1 => 'th\\is',
2 => '\\',
3 => 'Fo "o bar',
4 => '\\"B',
5 => 'ang Bling',
6 => 'foo',
7 => 'Boo',
8 => 'foo',
)
feyd: 0.45822906 seconds for 1000 iterations ( 0.00045823 second average )
array (
0 => 'bang',
1 => 'th\\\\is',
2 => '\\\\"Fo \\"o bar"',
3 => '\\\\\\"B"ang',
4 => 'Bling"',
5 => 'foo',
6 => 'Boo',
7 => 'foo',
)
Input:
Extra \s backslashes where they shouldn't be, plus an unterminated quote.
Ambush Commander: 0.13459802 seconds for 1000 iterations ( 0.00013460 second average )
array (
0 => 'Extra',
1 => 's',
2 => 'backslashes',
3 => 'where',
4 => 'they',
5 => 'shouldn\'t',
6 => 'be,',
7 => 'plus',
8 => 'an',
9 => 'unterminated',
10 => 'quote.',
)
feyd: 0.58185506 seconds for 1000 iterations ( 0.00058186 second average )
array (
0 => 'Extra',
1 => '\\s',
2 => 'backslashes',
3 => 'where',
4 => 'they',
5 => 'shouldn\'t',
6 => 'be,',
7 => 'plus',
8 => 'an',
9 => 'unterminated',
10 => 'quote.',
)
Ambush Commander and I disagree on stripping the slashes, but overall, they are the same output minus a few things here and there.