【问题标题】:regexp to remove anchor with specific class and it's content?正则表达式删除具有特定类及其内容的锚?
【发布时间】:2011-02-22 10:58:07
【问题描述】:
<a class="rsswidget" href="http:/domain.com/feed/" title="RSS">
<img style="border:0" width="14" height="14" src="http://domain.com/images/rss.png" alt="RSS">
</a>

我需要什么正则表达式来摆脱整个锚点,包括里面的图像? 不幸的是,我是一个正则表达式菜鸟,所以我需要你的帮助。非常感谢。

return preg_replace('#<a+class="rsswidget"[^>]*>.*?</a>#is', '', $content);

【问题讨论】:

标签: php regex preg-replace html-parsing


【解决方案1】:

这几乎是正确的。但+ 仅量化了a。你想在那个位置使用[^&gt;]+

preg_replace('#<a[^>]+class="rsswidget"[^>]*>.*?</a>#is',

为避免任何 regex-for-html-whatever-downvoting,这将是 QueryPath 替代方案:

return qp($html)->find("a.rsswidget")->remove()->writeHTML();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 2013-07-03
    • 2010-09-18
    • 1970-01-01
    相关资源
    最近更新 更多