php正则匹配url地址文章内容关键字自动添加链接

方法一:PHP数组,给文章加关键字链接,像163文章内容自动加链接的效果 
 
$keys =array( 
     array('网页特效','/js_a/js.html'), 
     array('seo','/seo/seo.html'), 
     array('php','/phper/php.html'), 
     array('jsp','/jsp/jsp.html'), 
     array('asp','/asp/asp.html'), 
     array('ps','/fw/photo.html'), 
     array('photoshop','/fw/photo.html'), 
     array('javascript','/js_a/js.html'), 
     array('.net','/net/net.html'), 
     array('非主流','/fw/photo.html'), 
     array('网络','/mon/mon.html'), 
     array('css','/cssdiv/css.html'), 
     array('平面设计','/fw/photo.html'), 
     array('网站','/person/'), 
     array('网页制作','/wy/yw.html'), 
     array('搜索引擎','/seo/seo.html'), 
     array('优化','/seo/seo.html'), 
     array('动画','/flash_a/flash.html'), 
     array('数据库','/database/database.html'), 
     array('挣钱','/mon/mon.html'), 
     array('运营','/mon/mon.html') 
); 
$str ="今天是2010年5月30号,我的网站出现的问题这对seo有很多的问题,seo就是搜索引擎优化了,以前学php好啊现在觉得jsp 好,css+div,网页,网页设计,网页制作,网页学习,网页教 学,Photoshop,Flash,HTML,CSS,Dreamweaver,Fireworks,ASP,PHP,JSP,ASP.NET,网站建 设,网站开发,网页特效,平面设计,个人网站,网页素材"; 
 
echo $str,"<br>"; 
foreach($keys as $nkeys){ 
       //print_r($nkeys);echo"<br>"; 
       //foreach( $nkeys as $join) { 
       //echo($join),"<br>"; 
       if(strpos($str,$nkeys[0]) ){ 
             $str =str_replace($nkeys[0],"<a href=http://www.****.com.cn".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);      
       } 
       //} 

     
   echo $str; 
 
 
 
方法二:自动给 URL添加链接 
 
 
Php代码  
<?php    
function autolink($foo)     
{    
$foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\1" target=_blank rel=nofollow>\1</a>', $foo);    
if( strpos($foo, "http") === FALSE ){    
$foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\1" target=_blank rel=nofollow >\1</a>', $foo);    
}else{    
$foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\1<a href="http://\2" target=_blank rel=nofollow>\2</a>', $foo);    
}    
return $foo;     
}    
?>   
 
调用这个函数来处理字符串如下:  
Php代码  
<?php    
$str = 'http://www.******.com';    
echo autolink($str);    
?> 
 
这里输出来的时候, http://www.***.com 这个网址就被加上链接了.效果就像 http://www.***.com 这一样的. 
 
注意哦,上面正则匹配里有一句 rel=nofollow ,这个是告诉搜索引擎们,这个链接不要去抓取,它和我们网站没有关系.这在处理广告链接上不失是一个好办法.

posted @ 2012-02-24 11:59  jia58960  阅读(3497)  评论(2编辑  收藏  举报