PHP過濾外部連結及外部圖片 添加rel="nofollow"屬性_PHP教程

來源:互聯網
上載者:User
原來站內很多文章都是摘錄的外部文章,文章裡很多連結要麼是時間久了失效了,要麼就是一些測試的網址,如:http://localhost/ 之類的,連結多了的話,就形成站內很多死連結,這對SEO最佳化是很不利的。那麼就需要對網站內的內容進行過濾,將不是內部連結的連結加上 rel="nofollow"屬性。

網上找到了wordpress的過濾外部連結的函數,將其改一下即可使用

//外部連結增加nofllow $content 內容 $domain 當前網站網域名稱function content_nofollow($content,$domain){ preg_match_all('/href="(.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,$domain)===false ) $content=str_replace('href="'.$val.'"', 'href="'.$val.'" rel="external nofollow" ',$content); } } preg_match_all('/src="(.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,$domain)===false ) $content=str_replace('src="'.$val.'"', 'src="'.$val.'" rel="external nofollow" ',$content); } } return $content;} 調用的時候很好調用,如下是調用示範 $a['content'] = content_nofollow($a['content'],$domain); //將文章內容裡的連結增加nofllow屬性 注意!過濾的網域名稱需要是不帶“/”的,如http://www.ledaokj.com 這樣才可以很好的過濾。 原文連結:增加對網站內容外部連結的過濾

http://www.bkjia.com/PHPjc/764698.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/764698.htmlTechArticle原來站內很多文章都是摘錄的外部文章,文章裡很多連結要麼是時間久了失效了,要麼就是一些測試的網址,如:http://localhost/ 之類的,連結...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.