$string = "[john] sometext [rohn] sometext [mohan]";
如何获取不同数组中的方括号之间的数据。
我有以下代码
preg_match_all("/\((?:[^()]|(?R))+\)/", $string , $matches);
上面的代码按照我需要的方式非常适合大括号,但是如何使它适合方括号?
答案 0 :(得分:0)
$string = "[john] sometext [rohn] sometext [mohan]";
preg_match_all("/\[([^]]+)\]/", $string , $matches);
print_r($matches);
输出:
Array
(
[0] => Array
(
[0] => [john]
[1] => [rohn]
[2] => [mohan]
)
[1] => Array
(
[0] => john
[1] => rohn
[2] => mohan
)
)
说明:
\[ : opening square bracket
( : start group 1
[^]]+ : 1 or more any character that is not closing square bracket
) : end group 1
\] : closing square racket