2017-04-14 63 views
-1

尝试捕获标记之间的所有文本。Php 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)) 
+0

使用非贪婪定量'' – Barmar

+0

不结果包括所有的'',一切之间''在(*?)? – Barmar

+0

使用查看源查看它实际返回的内容,因为浏览器隐藏了看起来像HTML标记的内容。 – Barmar

回答

2

您正则表达式的语法是贪婪。使用如下因素:

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

谢谢!但为什么??对不起,与正则表达式不是最好的! –

+0

@HunterS阅读教程,并特别关注有关贪婪的部分:http://www.regular-expressions.info/repeat.html – Barmar