0

I want to parse string in php with multiple delimiters I use this code

$pattern = '/[, ]/';

$array = preg_split( $pattern, $string);

This work fine , but I want to use and "[" and "]" as delimiters!!

How can I include this delimiters in $pattern. I try with $pattern = '/[, []]/'; and $pattern = '/[, ]/'; but error occurred.

Thanks

Jovan
  • 4,684
  • 13
  • 64
  • 98

1 Answers1

0

Escaping the brackets should work.

\[

Related: preg_quote()

Edit: use json_decode() instead.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I want to parse this:[41103697,20807640, 41099181,20804771, 41099064,20804712], delimiters are "[],space". When I put `\[` $array[0] is empty string ,. and $array[3] is empty... Why? Thanks – Jovan Feb 22 '11 at 21:49
  • @Jovan what kind of data is that? The example you show could be parsed using `json_decode()` – Pekka Feb 22 '11 at 21:51