php解析具有多个分隔符的字符串

php解析具有多个分隔符的字符串

问题描述:

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

我想在php中用多个分隔符解析字符串 我使用此代码 p> $ pattern ='/ [,] /'; $ array = preg_split($ pattern,$ string); code> pre>

这 工作正常,但我想使用“[”和“]”作为分隔符!! p>

如何在$ pattern中包含此分隔符。 我尝试使用 $ pattern ='/ [,[]] /'; code>和 $ pattern ='/ [,] /'; code>但发生了错误。 p>

谢谢 p> div>

Escaping the brackets should work.

\[

Related: preg_quote()

Edit: use json_decode() instead.