开发者

regexp to remove anchor with specific class and it's content?

开发者 https://www.devze.com 2023-02-12 07:45 出处:网络
<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 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>

What regexp do I need to get rid of this entire anchor, inclusiveley the image inside? Unfortunately I'm a regexp noob and so I need your help. Thank you very much.

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


That's almost right. But the + quantifies only the a. You wanted to use [^>]+ at that position:

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

To avoid any regex-for-html-whatever-downvoting, this would be the QueryPath alternative:

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

精彩评论

暂无评论...
验证码 换一张
取 消