Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP explode the string, but treat words in quotes as a single word

How can I explode the following string:

Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor 

into

array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor") 

So that the text in quotation is treated as a single word.

Here's what I have for now:

$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor" $noquotes = str_replace("%22", "", $mytext"); $newarray = explode(" ", $noquotes); 

but my code divides each word into an array. How do I make words inside quotation marks treated as one word?

like image 691
timofey Avatar asked Feb 04 '10 19:02

timofey


People also ask

Can you use single quotes for strings in PHP?

Single quoted ¶The simplest way to specify a string is to enclose it in single quotes (the character ' ). To specify a literal single quote, escape it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ).

What does explode () do in PHP?

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Note: This function is binary-safe.


1 Answers

You could use a preg_match_all(...):

$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing \\"elit" dolor'; preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $text, $matches); print_r($matches); 

which will produce:

Array (     [0] => Array         (             [0] => Lorem             [1] => ipsum             [2] => "dolor sit amet"             [3] => consectetur             [4] => "adipiscing \"elit"             [5] => dolor         )  ) 

And as you can see, it also accounts for escaped quotes inside quoted strings.

EDIT

A short explanation:

"           # match the character '"' (?:         # start non-capture group 1    \\        #   match the character '\'   .         #   match any character except line breaks   |         #   OR   [^\\"]    #   match any character except '\' and '"' )*          # end non-capture group 1 and repeat it zero or more times "           # match the character '"' |           # OR \S+         # match a non-whitespace character: [^\s] and repeat it one or more times 

And in case of matching %22 instead of double quotes, you'd do:

preg_match_all('/%22(?:\\\\.|(?!%22).)*%22|\S+/', $text, $matches); 
like image 52
Bart Kiers Avatar answered Sep 17 '22 00:09

Bart Kiers