Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP preg_split with two delimiters unless a delimiter is within quotes

Further on from my previous question about preg_split which was answers super fast, thanks to nick; I would really like to extend the scenario to no split the string when a delimiter is within quotes. For example:

If I have the string foo = bar AND bar=foo OR foobar="foo bar", I'd wish to split the sting on every space or = character but include the = character in the returned array (which works great currently), but I don't want to split the string either of the delimiters are within quotes.

I've got this so far:

<!doctype html>
<?php

$string = 'foo = bar AND bar=foo';

$array = preg_split('/ +|(=)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

?>
<pre>
<?php

print_r($array);

?>
</pre>

Which gets me:

Array
(
    [0] => foo
    [1] => =
    [2] => bar
    [3] => AND
    [4] => bar
    [5] => =
    [6] => foo
)

But if I changed the string to:

$string = 'foo = bar AND bar=foo OR foobar = "foo bar"';

I'd really like the array to be:

Array
(
    [0] => foo
    [1] => =
    [2] => bar
    [3] => AND
    [4] => bar
    [5] => =
    [6] => foo
    [6] => OR
    [6] => foobar
    [6] => =
    [6] => "foo bar"
)

Notice the "foo bar" wasn't split on the space because it's in quotes?

Really not sure how to do this within the RegEx or if there is even a better way but all your help would be very much appreciated!

Thank you all in advance!

like image 621
Jonathon Oates Avatar asked Feb 20 '23 14:02

Jonathon Oates


1 Answers

Try

$array = preg_split('/(?: +|(=))(?=(?:[^"]*"[^"]*")*[^"]*$)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

The

(?=(?:[^"]*"[^"]*")*[^"]*$)

part is a lookahead assertion making sure that there is an even number of quote characters ahead in the string, therefore it will fail if the current position is between quotes:

(?=      # Assert that the following can be matched:
 (?:     # A group containing...
  [^"]*" #  any number of non-quote characters followed by one quote
  [^"]*" #  the same (to ensure an even number of quotes)
 )*      # ...repeated zero or more times,
 [^"]*   # followed by any number of non-quotes
 $       # until the end of the string
)
like image 91
Tim Pietzcker Avatar answered Mar 05 '23 17:03

Tim Pietzcker