php正则替换非站内链接
<?php //要替换的文本,比如产品描述中的文字 header("content-Type: text/html; charset=utf-8"); $content = ‘ <li><a href="http://afish.cnblogs.com" target="_blank">小鱼阁</a></li> <li><a href="http://afish.cnblogs.com" target="_blank">小鱼阁</a></li> <li><a href="http://afish.cnblogs.com" target="_blank">小鱼阁</a></li> <li><a href="http://www.baidu.com" target="_blank">百度</a></li> <li><a href="http://www.baidu.com" target="_blank">百度</a></li> <li><a href="http://www.baidu.com" target="_blank">百度</a></li> <li><a href="http://www.soso.com" target="_blank">搜搜</a></li> <li><a href="http://www.soso.com" target="_blank">搜搜</a></li> <li><a href="http://www.soso.com" target="_blank">搜搜</a></li> ‘; //本站域名 $host_url = str_replace(".", "\.", str_replace("www.", "", $_SERVER[‘HTTP_HOST‘])); //echo $host_url; //替换正则 $pattern = ‘/<[^>]*href=[\‘\"]http?:\/\/(?!‘.str_replace("http://","",$host_url).‘|‘.str_replace("http://www.","",$host_url).‘|[\w\_]+\.‘.str_replace("http://www.","",$host_url).‘)[^\‘^\"]*[\‘\"][^>]*>(.+?)<\/a>/is‘; //替换成的内容 //$replace = ‘<a href="http://‘.$_SERVER[‘HTTP_HOST‘].‘" target="_blank">$1</a>‘; $replace = ‘<a href="http://‘.$_SERVER[‘HTTP_HOST‘].‘" target="_blank">本站链接</a>‘; //进行替换 $content = preg_replace($pattern, $replace, $content); //输出替换后的内容 echo $content; ?>
时间: 2024-11-07 17:19:35