Php preg_match_all的行为不符合预期

时间:2017-04-14 01:54:49

标签: php html regex preg-match preg-match-all

尝试捕获标签之间的所有文字。

代码:

$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>';

$match = '/<test>(.*)<tests>/';

preg_match_all($match, $test, $nextLink);

print_r的结果:

Array ( [0] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) [1] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) ) 

1 个答案:

答案 0 :(得分:2)

你的正则表达式语法是贪婪的。使用下面的内容:

 $match = '/<test>(.*?)<tests>/';