I need to split string by spaces, but phrase in quotes should be preserved unsplitted. Example:
word1 word2 \"this is a phrase\" word3 word4 \"this is a s
assuming your quotes are well defined, ie, in pairs, you can explode and go through for loop every 2 fields. eg
$str = "word1 word2 \"this is a phrase\" word3 word4 \"this is a second phrase\" word5 word6 \"lastword\"";
print $str ."\n";
$s = explode('"',$str);
for($i=1;$i
output
$ php test.php
Spaces found: this is a phrase
Spaces found: this is a second phrase
No complicated regexp required.