《PHP編程:PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過(guò)濾方法》要點(diǎn):
本文介紹了PHP編程:PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過(guò)濾方法,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP實(shí)戰(zhàn)熟悉SEO的朋友都知道,對(duì)于網(wǎng)站外部鏈接失效的情況如果鏈接帶有rel="nofollow"屬性可以避免不必要的損失.本文就以實(shí)例形式演示了PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過(guò)濾辦法.具體如下:
PHP實(shí)戰(zhàn)問(wèn)題描述:原來(lái)站內(nèi)很多文章都是摘錄的外部文章,文章里很多鏈接要么是時(shí)間久了失效了,要么就是一些測(cè)試的網(wǎng)址,如:http://localhost/ 之類(lèi)的,鏈接多了的話,就形成站內(nèi)很多死鏈接,這對(duì)SEO優(yōu)化是很不利的.
PHP實(shí)戰(zhàn)解決辦法:需要對(duì)站點(diǎn)內(nèi)的內(nèi)容進(jìn)行過(guò)濾,將不是內(nèi)部鏈接的鏈接加上 rel="nofollow"屬性.
PHP實(shí)戰(zhàn)本文借鑒了wordpress的過(guò)濾外部鏈接的函數(shù),將其改一下即可使用.
PHP實(shí)戰(zhàn)具體代碼如下:
PHP實(shí)戰(zhàn)
//外部鏈接增加nofllow $content 內(nèi)容 $domain 當(dāng)前網(wǎng)站域名
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;
}
PHP實(shí)戰(zhàn)調(diào)用的時(shí)候很好調(diào)用,如下是調(diào)用演示
PHP實(shí)戰(zhàn)
$a['content'] = content_nofollow($a['content'],$domain); //將文章內(nèi)容里的鏈接增加nofllow屬性
PHP實(shí)戰(zhàn)?
注意!這里過(guò)濾的域名需要是不帶“/”的,如,這樣才可以很好的過(guò)濾.
PHP實(shí)戰(zhàn)相信本文所述的辦法對(duì)大家的PHP項(xiàng)目開(kāi)發(fā)有一定的借鑒價(jià)值.
《PHP編程:PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過(guò)濾方法》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP編程:PHP實(shí)現(xiàn)對(duì)站點(diǎn)內(nèi)容外部鏈接的過(guò)濾方法》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/14724.html