如何取出字符串中的某个字符并将它们放在一个数组中,如:
"{2} in better that {1} when it comes to blah blah blah"
,输出为:
array(0 => "2", 1 => "1");
我使用了正则表达式,但似乎它没有在整个字符串中循环,或者我可能错过了什么?
由于
答案 0 :(得分:4)
使用preg_match_all
代替preg_match:
<?php
$str = "{2} in better that {1} when it comes to blah blah blah";
preg_match_all('/{\d+}/', $str, $matches);
print_r($matches[0]);
?>
在我的机器上显示:
Array
(
[0] => {2}
[1] => {1}
)
答案 1 :(得分:1)
preg_match_all('/\{\d+\}/', $yourString, $matches);
var_dump($matches);