PHP preg_split with two delimiters unless a delimiter is within quotes

﹥>﹥吖頭↗ 提交于 2019-12-22 10:57:41

问题


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!


回答1:


I was able to do this by adding quoted strings as a delimiter a-la

"(.*?)"| +|(=)

The quoted part will be captured. It seems like this is a bit tenuous and I did not test it extensively, but it at least works on your example.




回答2:


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
)



回答3:


But why bother splitting?

After a look at this old question, this simple solution comes to mind, using a preg_match_all rather than a preg_split. We can use this simple regex to specify what we want:

"[^"]*"|\b\w+\b|=

See online demo.



来源:https://stackoverflow.com/questions/11873240/php-preg-split-with-two-delimiters-unless-a-delimiter-is-within-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!