【发布时间】:2009-12-14 16:36:45
【问题描述】:
如何取出字符串中的某个字符并将它们全部放在一个数组中,例如:
"{2} in better that {1} when it comes to blah blah blah"
输出将是:
array(0 => "2", 1 => "1");
我使用了正则表达式,但它似乎没有在整个字符串中循环,或者我可能遗漏了什么?
谢谢
【问题讨论】:
如何取出字符串中的某个字符并将它们全部放在一个数组中,例如:
"{2} in better that {1} when it comes to blah blah blah"
输出将是:
array(0 => "2", 1 => "1");
我使用了正则表达式,但它似乎没有在整个字符串中循环,或者我可能遗漏了什么?
谢谢
【问题讨论】:
使用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}
)
【讨论】:
preg_match_all('/\{\d+\}/', $yourString, $matches);
var_dump($matches);
【讨论】: