【发布时间】:2010-05-24 09:58:08
【问题描述】:
假设我们有这样的 HTML 代码。我们需要获取所有不包含img标签的<a href=""></a>标签。
<a href="http://domain1.com"><span>Here is link</span></a>
<a href="http://domain2.com" title="">Hello</a>
<a href="http://domain3.com" title=""><img src="" /></a>
<a href="http://domain4" title=""> I'm the image <img src="" /> yeah</a>
我正在使用这个正则表达式来查找所有 a 标签链接:
preg_match_all("!<a[^>]+href=\"?'?([^ \"'>]+)\"?'?[^>]*>(.*?)</a>!is", $content, $out);
我可以这样修改:
preg_match_all("!<a[^>]+href=\"?'?([^ \"'>]+)\"?'?[^>]*>([^<>]+?)</a>!is", $content, $out);
但我如何告诉它排除包含<img 子字符串的结果在<a href=""></a> 内?
【问题讨论】:
-
不要使用正则表达式解析 HTML :-)
-
我同意你的观点,但我仍然对使用正则表达式从结果中排除某些单词的方法感兴趣。
标签: php regex regex-negation