批量过滤MYSQL数据库内站外链接和图片程序代码
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); //修正当前域名网址
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.'更新成功!
';}
}
//外部链接增加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="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;
}
PHP批量过滤MYSQL数据库内站外链接和图片