duanniying2342 2014-06-24 15:44
浏览 123
已采纳

PHP - 如果href或src不以http,https或www开头,则删除特定的标签或img标签

I want to remove specific a and img tags from the $string_1 if <src> or <href> does not start with www, http or https inside <a> or <img> tags.

For example, $string_1 is converted to $string_2 by removing:

<img src="/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/>

and

<a href="/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth &gt;</a>

because the src and href tags do not start with http, https or www.

$string_1 = '
<div class="mainpost"><p><img src="/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/></p>
<div class="mainpost"><p><img src="http://www.domain.com/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/></p>
<p><a href="http://domain.com/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth &gt;</a></p>
<p>Photography Business Growth | With a world wide recession, photographers and small business owners are forced, more than ever, to think creatively, to think differently and outside of the box. With very little or no money to invest in your business, can you move forward? How can you build your brand and make sure to get happier, paying clients through your door?<br/><span id="more-609494"/></p>
<p>If you take good shots it doesn’t mean you’ll gain success and popularity among customers. For those of you who have survived start=up and built successful brands, you may be wondering which step to take next to grow your business beyond its current status. There are numerous possibilities, some of which we’ll outline here. You need to know how to sell yourself well! Everything is quite simple and you can do it yourself.</p>
<p><a href="/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth &gt;</a></p>
';

$string_2= '
<div class="mainpost"><p></p>
<div class="mainpost"><p><img src="http://www.domain.com/wp-content/uploads/2014/06/photography-business-2.jpg" alt="photography business growth 1 650x430 6 Simple Ways To Help Grow Your Photography Business" width="650" height="430" class="alignnone size-large wp-image-609513" title="6 Simple Ways To Help Grow Your Photography Business"/></p>
<p><a href="http://domain.com/photography-business-growth/" rel="nofollow">Read more about Photography Business Growth &gt;</a></p>
<p>Photography Business Growth | With a world wide recession, photographers and small business owners are forced, more than ever, to think creatively, to think differently and outside of the box. With very little or no money to invest in your business, can you move forward? How can you build your brand and make sure to get happier, paying clients through your door?<br/><span id="more-609494"/></p>
<p>If you take good shots it doesn’t mean you’ll gain success and popularity among customers. For those of you who have survived start=up and built successful brands, you may be wondering which step to take next to grow your business beyond its current status. There are numerous possibilities, some of which we’ll outline here. You need to know how to sell yourself well! Everything is quite simple and you can do it yourself.</p>
';

Could you please help me to solve this problem? Thanks

  • 写回答

3条回答 默认 最新

  • douhui4831 2014-06-24 16:11
    关注

    Here is a first approach in PHP. It works for your example data. In $string_2 was the tailing "<p></p>" missing.

    $string_3 = $string_1;
    $pattern = "([^wh]|w[^w]|ww[^w]|h[^t]|ht[^t]|htt[^p])";
    $string_3 = preg_replace("/<img src=\"".$pattern."[^>]*>/","",$string_3);
    $string_3 = preg_replace("/<a href=\"".$pattern."[^>]*>[^<]*<\/a>/","",$string_3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料