首頁  >  文章  >  後端開發  >  php如何去除外鏈

php如何去除外鏈

coldplay.xixi
coldplay.xixi原創
2020-09-01 13:35:512565瀏覽

php移除外鏈的方法:將連結加上【rel="nofollow"】屬性,程式碼為【$a['content'] = content_nofollow($a['content'],$domain) ;】。

php如何去除外鏈

【相關學習推薦:#php圖文教學##】

php移除外鏈的方法:

解決方法:需要對網站內的內容進行過濾,將不是內部連結的連結加上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属性

#相關學習推薦:

php程式設計 (影片)

以上是php如何去除外鏈的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn