2

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] => 'xyz=1'
Morpha
  • 23
  • 1
  • 3

2 Answers2

8

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)
cypher
  • 6,822
  • 4
  • 31
  • 48
0
parse_str(str_replace('||','&',$str),$arr);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345