溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php去除外鏈的方法

發(fā)布時間:2020-09-04 10:34:36 來源:億速云 閱讀:169 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關php去除外鏈的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

php去除外鏈的方法:將不是內部鏈接的鏈接加上【rel="nofollow"】屬性,代碼為【$a['content'] = content_nofollow($a['content'],$domain);】。

php去除外鏈的方法

php去除外鏈的方法:

解決方法:需要對站點內的內容進行過濾,將不是內部鏈接的鏈接加上 rel="nofollow"屬性。

本文借鑒了wordpress的過濾外部鏈接的函數(shù),將其改一下即可使用。

具體代碼如下:

//外部鏈接增加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去除外鏈的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

php
AI