国产精品成人VA在线观看,亚洲日韩在线中文字幕综合,亚洲AV电影天堂男人的天堂,久久人人爽人人爽人人av东京热

News新聞

業(yè)界新聞動(dòng)態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁      樂道系統(tǒng)FAQ      PHP批量過濾MYSQL數(shù)據(jù)庫(kù)內(nèi)站外鏈接和圖片

PHP批量過濾MYSQL數(shù)據(jù)庫(kù)內(nèi)站外鏈接和圖片

標(biāo)簽: 發(fā)布日期:2014-05-04 22:54:00 1581
【CLI】利用Curl下載文件實(shí)時(shí)進(jìn)度條顯示的實(shí)現(xiàn)

因發(fā)現(xiàn)站內(nèi)很多引用站外文章的鏈接失效,產(chǎn)生大量的死鏈接,對(duì)于搜索引擎來說是極不友好的,很不利于網(wǎng)站優(yōu)化,所以站內(nèi)添加了站外鏈接過濾功能,對(duì)于新加的文章,在添加入庫(kù)時(shí)就自動(dòng)增加rel="nofollow"標(biāo)簽,見文章《增加對(duì)站點(diǎn)內(nèi)容外部鏈接的過濾》。因考慮如果是在前臺(tái)調(diào)用數(shù)據(jù)時(shí)過濾的話,對(duì)網(wǎng)頁打開速度,服務(wù)器能耗都增加許多,所以就采用的是入庫(kù)時(shí)添加。

那么,原來已有的數(shù)據(jù)怎么辦?現(xiàn)在需要對(duì)原來的數(shù)據(jù)也進(jìn)行此操作,如果是在后臺(tái)一條條編輯來實(shí)現(xiàn),即使只需要點(diǎn)一下,工程量也是很大的,那么就需要一個(gè)批處理操作。

寫一個(gè)批處理程序即可,經(jīng)調(diào)試,測(cè)試,以下的程序可很好的替換原來數(shù)據(jù)庫(kù)里面的外部鏈接和外部圖片

如,站點(diǎn)是http://www.9u769.cn

一篇文章里有一個(gè)鏈接是 http://www.53sj.net/article-6-1.html

一個(gè)圖片是 http://www.53sj.net/data/attachment/block/d3/d34780d1fca3d6b7960a7eb7a2c4c0d3.jpg

經(jīng)過批處理操作后

其代碼變成 <a href="http://www.53sj.net/article-6-1.html" rel="external nofollow"

<img src="http://www.53sj.net/data/attachment/block/d3/d34780d1fca3d6b7960a7eb7a2c4c0d3.jpg" rel="external nofollow"

 

批量過濾MYSQL數(shù)據(jù)庫(kù)內(nèi)站外鏈接和圖片程序代碼

global $config,$db;
$sql = "SELECT `id`,`content` FROM `{$db->prefix}article`";
 
$a_list = $db->query($sql);
 
$domain = $config['url'];
$domain = substr($domain,0,strlen($domain)-1);  //修正當(dāng)前域名網(wǎng)址
 
foreach($a_list as $a){
$content = content_nofollow($a['content'],$domain);
update_a($a['id'],addslashes($content));
}
exit;
 
function update_a($id,$content){
global $config,$db;
 
$sql = "update `{$db->prefix}article` SET `content`='{$content}' where `id`={$id}";
if($db->execute($sql)){echo $id.'更新成功!<br />';}
}
 
//外部鏈接增加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="http:(.*?)"/',$content,$matches);
 if($matches){
  foreach($matches[1] as $val){
   if( strpos($val,$domain)===false ) $content=str_replace('src="http:'.$val.'"', 'src="http:'.$val.'" rel="external nofollow" ',$content);
  }
 }
 return $content;
}