Take the 2-minute tour ×

How can I explode the following string:

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


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?

share|improve this question
This sounds like a job for Regex –  Earlz Feb 4 '10 at 19:10
add comment

4 Answers

up vote 36 down vote accepted

You could use a preg_match_all(...):

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

which will produce:

    [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.


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);
share|improve this answer
Is there a reason not to use preg_split instead of preg_match_all? it seems like a more natural fit IMO. –  prodigitalson Feb 4 '10 at 19:20
That's Awesome! I'll have to study the code for a bit to figure what just happened! thanks –  timofey Feb 4 '10 at 19:21
@prodigitalson: no, using preg_split(...) you cannot account for escaped characters. preg_match_all(...) "behaves" more like a parser which is the more natural thing to do here. Besides, using a preg_split(...), you'll need to look ahead on each space to see how many quotes are ahead of it, making it an O(n^2) operation: no problem for small strings, but might decrease the runtime when larger strings are involved. –  Bart Kiers Feb 4 '10 at 19:31
@timofey, see my edit. Don't hesitate to ask for more clarification if it's not clear to you: you're the one maintaining the code, so you should understand it (and I'm more than happy to provide extra information if it's needed). –  Bart Kiers Feb 4 '10 at 19:36
Thanks Bart K.! I was already searching google for answers on that one:) –  timofey Feb 4 '10 at 19:39
show 12 more comments

This would have been much easier...

$test = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor';
var_dump(str_getcsv($test, ' '));

Gives you

array(6) {
  string(5) "Lorem"
  string(5) "ipsum"
  string(14) "dolor sit amet"
  string(11) "consectetur"
  string(15) "adipiscing elit"
  string(5) "dolor"
share|improve this answer
This works on my development machine, but not on my production server. :-/ –  queueoverflow Mar 17 '12 at 18:22
str_getcsv requires PHP 5.3. –  armakuni Aug 2 '13 at 6:18
add comment

You can also try this multiple explode function

function multiexplode ($delimiters,$string)

return  $launch;

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);

share|improve this answer
add comment

Descriptive answer:

  1. Match all spaces between quotes and replace them with an underscore (preg_match case)
  2. use explode(" ",$result_from_step1)
  3. now replace the underscores with spaces

Rest i believe you can figure out easily :)


share|improve this answer
How do I select the text or spaces between the quotes? –  timofey Feb 4 '10 at 19:16
i would recommend using a regular expression for that. –  andreas Feb 4 '10 at 19:17
this would lose data if your input string included underscores (since they would end up as spaces) –  rmeador Feb 4 '10 at 20:18
add comment

Your Answer


