【问题标题】:Php preg_match_all does not behave as expected [duplicate]Php preg_match_all 的行为不符合预期[重复]
【发布时间】:2017-04-14 01:54:48
【问题描述】:

试图捕获标签之间的所有文本。

代码:

$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 ) ) 

【问题讨论】:

  • 使用非贪婪量化(.*?)
  • 结果不包括所有&lt;test&gt;&lt;tests&gt; 之间的所有内容吗?
  • 使用查看源代码查看它实际返回的内容,因为浏览器隐藏了看起来像 HTML 标记的内容。

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


【解决方案1】:

您的正则表达式语法很贪心。使用如下:

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

【讨论】:

猜你喜欢
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多