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?
You could use a
preg_match_all(...)
:which will produce:
And as you can see, it also accounts for escaped quotes inside quoted strings.
EDIT
A short explanation:
And in case of matching
%22
instead of double quotes, you'd do: