How to explode array by multiple delimiter characters
I have a string like this:
abc=1&def=abc||abc=xyz&xyz=1
How can I explode it by the &
and ||
characters?
for eg in this case the array should be
[0] => 'abc=1'
[1] => 'def=abc'
[2] => 'abc=xyz'
[3] => 'xy开发者_Python百科z=1'
Use preg_split:
$str = 'abc=1&def=abc||abc=xyz&xyz=1';
$arr = preg_split('#(&|[\|]{2})#', $str);
var_dump($arr);
will produce
array
0 => string 'abc=1' (length=5)
1 => string 'def=abc' (length=7)
2 => string 'abc=xyz' (length=7)
3 => string 'xyz=1' (length=5)
parse_str(str_replace('||','&',$str),$arr);
精彩评论