Regular Expressions and Search

i need to split words and add to a array from filter input.
i can split words with [ ] (Space character). But also i need to not split “word1 word2” with space character.
for example: Filter input “word1 word2” word3 word4
my array will need to be like this after split
arr[0] = word1 word2
arr[1] = word3
arr[2] = word4

can some one help me about this?

By the way im sorry for my poor english.


The easiest solution would be to “split” the string using preg_match_all() function, like this:


<?php $content = '"word1 word2" word3 word4'; if (preg_match_all('/[^\s"]+|"([^"]*)"/', $content, $matches)){ echo '
    echo '
'; } ?>


The above code will produce the following output:

    [0] => "word1 word2"
    [1] => word3
    [2] => word4

And here is a brief explanation of regular expression: [^\s"]+|"([^"]*)"

  • any non-empty string that does not contain spaces and double quotes
  • any string that does not contain double quotes, but is surrounded by double quotes
Sponsor our Newsletter | Privacy Policy | Terms of Service