我想匹配被%包围的子字符串,但是preg_match_all
似乎会在同一行中同时包含多个。
代码如下:
preg_match_all("/%.*%/", "%hey%_thereyou're_a%rockstar%\nyo%there%", $matches);
print_r($matches);
以下是产生的输出结果。
Array
(
[0] => Array
(
[0] => %hey%_thereyou're_a%rockstar%
[1] => %there%
)
)
然而,我希望它能生成以下数组:
[0] => %hey%
[1] => %rockstar%
[2] => %there%
我错过了什么?
/U
反转正则表达式中所有量词的贪婪性,因此/%.+%/U
与/%.+?%/
相同,而/%.+?%/U
与/%.+%/
相同。http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php - Alan Moore