| 
       网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的方法在最上面。 
例1,代码如下: 
	
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-   
 
	- function yang_keyword_link($content,$keyword,$link){  
 
	-      
 
	-     $content = preg_replace( '|(<img[^>]*?)('.$keyword.')([^>]*?>)|U', '$1%&&&&&%$3', $content);  
 
	-     $regEx = '/(?!((<.*?)|(<a.*?)))('.$keyword.')(?!(([^<>]*?)>)|([^>]*?</a>))/si';  
 
	-     $url='<a href="'.$link.'">'.$keyword.'</a>';  
 
	-     $content = preg_replace($regEx,$url,$content,1);  
 
	-      
 
	-     $content=str_replace('%&&&&&%',$keyword,$content);  
 
	-     return $content;  
 
	- } 
 
	 
 
例2,代码如下: 
	
	- include_once(dirname(__file__)."/../db/DBViewSpot.php" ); 
 
	- class InnerLink{ 
 
	-     private static $spotUrlMap; 
 
	-      
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-     public static function genSpotLink($basePath, $description) 
 
	-     { 
 
	-         if(emptyempty(InnerLink::$spotUrlMap)){ 
 
	-             InnerLink::$spotUrlMap = DBViewSpot::getSpotPare(); 
 
	-         } 
 
	-          
 
	-         if ( emptyempty($description)) { 
 
	-             return $description; 
 
	-         } 
 
	-         foreach (InnerLink::$spotUrlMap AS $spotUrlPair){ 
 
	-             $replace = "<a target='_blank' href='http://pzg412403.blog.163.com/blog/".$basePath."/".$spotUrlPair[1].".html'>".$spotUrlPair[0]."</a>"; 
 
	-              
 
	-             $tmp1 = explode("<a",$description); 
 
	-             $is_replaced=false; 
 
	-             foreach ($tmp1 as $key=>$item){ 
 
	-                 $tmp2 = explode("</a>",$item); 
 
	-                 if (sizeof($tmp2)>1) { 
 
	-                     if (substr($tmp2[0],0,1)!="a" && substr($tmp2[0],0,1)!="A"){ 
 
	-                         if ($is_replaced===false) { 
 
	-                             $tmp2[1] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$tmp2[1],$is_replaced); 
 
	-                         } 
 
	-                         $tmp1[$key] = implode("</a>",$tmp2); 
 
	-                     } 
 
	-                 }else { 
 
	-                     if (is_string($item) && $is_replaced===false) { 
 
	-                         $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$item,$is_replaced); 
 
	-                     } 
 
	-                 } 
 
	-             } 
 
	-             $description = implode("<a",$tmp1); 
 
	-         } 
 
	-         return $description; 
 
	-     } 
 
	-      
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-  
 
	-     private static function str_replace_once($needle, $replace, $haystack,&$is_replaced) { 
 
	-         $pos = strpos($haystack, $needle); 
 
	-         if ($pos === false) { 
 
	-             return $haystack; 
 
	-         } 
 
	-         $is_replaced=true; 
 
	-         return substr_replace($haystack, $replace, $pos, strlen($needle)); 
 
	-     } 
 
	- } 
 
	 
 
例3,这个是自己最初学php时写的,感觉有点问题,代码如下: 
	
	- <?php   
 
	-     $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){   
 
	-             
 
	-             
 
	-             
 
	-            if(strpos($str,$nkeys[0]) ){   
 
	-                  $str =str_replace($nkeys[0],"<a href=http://www.vcphp.com".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);       
 
	-            }   
 
	-            
    |