最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php实现文章内容关键字增加内链

时间:2014-06-09 16:39来源: 作者: 点击:
网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的方法在最上面。 例1,代码如下: /** *对内容中的关键词添加

网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的方法在最上面。

例1,代码如下:

  1. /**  
  2. *对内容中的关键词添加链接  
  3. *只处理第一次出现的关键词,对已有链接的关键不会再加链接,支持中英文  
  4. *$content:string 原字符串  
  5. *$keyword:string  关键词  
  6. *$link:string,链接  
  7. */  
  8. function yang_keyword_link($content,$keyword,$link){  
  9.     //排除图片中的关键词  
  10.     $content = preg_replace( '|(<img[^>]*?)('.$keyword.')([^>]*?>)|U''$1%&&&&&%$3'$content);  
  11.     $regEx = '/(?!((<.*?)|(<a.*?)))('.$keyword.')(?!(([^<>]*?)>)|([^>]*?</a>))/si';  
  12.     $url='<a href="'.$link.'">'.$keyword.'</a>';  
  13.     $content = preg_replace($regEx,$url,$content,1);  
  14.     //还原图片中的关键词  
  15.     $content=str_replace('%&&&&&%',$keyword,$content);  
  16.     return $content;  

例2,代码如下:

  1. include_once(dirname(__file__)."/../db/DBViewSpot.php" ); 
  2. class InnerLink{ 
  3.     private static $spotUrlMap
  4.     /** 
  5.      * Generate view spots keywords link 
  6.      * 
  7.      * @param string $description 
  8.      * @param array $spotUrlMap 
  9.      * @return string 
  10.      */ 
  11.     public static function genSpotLink($basePath$description
  12.     { 
  13.         if(emptyempty(InnerLink::$spotUrlMap)){ 
  14.             InnerLink::$spotUrlMap = DBViewSpot::getSpotPare(); 
  15.         } 
  16.         // 排除不规则数据 
  17.         if ( emptyempty($description)) { 
  18.             return $description
  19.         } 
  20.         foreach (InnerLink::$spotUrlMap AS $spotUrlPair){ 
  21.             $replace = "<a target='_blank' href='http://pzg412403.blog.163.com/blog/".$basePath."/".$spotUrlPair[1].".html'>".$spotUrlPair[0]."</a>"
  22.             // 描述里面只有文字,没有图片,所以只要注意 a 链接 
  23.             $tmp1 = explode("<a",$description); 
  24.             $is_replaced=false; 
  25.             foreach ($tmp1 as $key=>$item){ 
  26.                 $tmp2 = explode("</a>",$item); 
  27.                 if (sizeof($tmp2)>1) { 
  28.                     if (substr($tmp2[0],0,1)!="a" && substr($tmp2[0],0,1)!="A"){ 
  29.                         if ($is_replaced===false) { 
  30.                             $tmp2[1] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$tmp2[1],$is_replaced); 
  31.                         } 
  32.                         $tmp1[$key] = implode("</a>",$tmp2); 
  33.                     } 
  34.                 }else { 
  35.                     if (is_string($item) && $is_replaced===false) { 
  36.                         $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$item,$is_replaced); 
  37.                     } 
  38.                 } 
  39.             } 
  40.             $description = implode("<a",$tmp1); 
  41.         } 
  42.         return $description
  43.     } 
  44.     /** 
  45.      * replace key word for one time 
  46.      * 
  47.      * @param string $needle 
  48.      * @param string $replace 
  49.      * @param string $haystack 
  50.      * @param bool $is_replaced 
  51.      * @return string 
  52.      */ 
  53.     private static function str_replace_once($needle$replace$haystack,&$is_replaced) { 
  54.         $pos = strpos($haystack$needle); 
  55.         if ($pos === false) { 
  56.             return $haystack
  57.         } 
  58.         $is_replaced=true; 
  59.         return substr_replace($haystack$replace$posstrlen($needle)); 
  60.     } 

例3,这个是自己最初学php时写的,感觉有点问题,代码如下:

  1. <?php   
  2.     $keys =array(   
  3.          array('网页特效','/js_a/js.html'),   
  4.          array('seo','/seo/seo.html'),   
  5.          array('php','/phper/php.html'),   
  6.          array('jsp','/jsp/jsp.html'),   
  7.          array('asp','/asp/asp.html'),   
  8.          array('ps','/fw/photo.html'),   
  9.          array('photoshop','/fw/photo.html'),   
  10.          array('javascript','/js_a/js.html'),   
  11.          array('.net','/net/net.html'),   
  12.          array('非主流','/fw/photo.html'),   
  13.          array('网络','/mon/mon.html'),   
  14.          array('css','/cssdiv/css.html'),   
  15.          array('平面设计','/fw/photo.html'),   
  16.          array('网站','/person/'),   
  17.          array('网页制作','/wy/yw.html'),   
  18.          array('搜索引擎','/seo/seo.html'),   
  19.          array('优化','/seo/seo.html'),   
  20.          array('动画','/flash_a/flash.html'),   
  21.          array('数据库','/database/database.html'),   
  22.          array('挣钱','/mon/mon.html'),   
  23.          array('运营','/mon/mon.html')   
  24.              
  25.     );   
  26.            
  27.     $str ="今天是2010年5月30号,我的网站出现的问题这对seo有很多的问题,seo就是搜索引擎优化了,以前学php好啊现在觉得jsp好,css+div,网页,网页设计,网页制作,网页学习,网页教学,Photoshop,Flash,HTML,CSS,Dreamweaver,Fireworks,ASP,PHP,JSP,ASP.NET,网站建设,网站开发,网页特效,平面设计,个人网站,网页素材";   
  28.       
  29.       
  30.     echo $str,"<br>";   
  31.     foreach($keys as $nkeys){   
  32.            //print_r($nkeys);echo"<br>";   
  33.            //foreach( $nkeys as $join) {   
  34.            //echo($join),"<br>";   
  35.            if(strpos($str,$nkeys[0]) ){   
  36.                  $str =str_replace($nkeys[0],"<a href=http://www.vcphp.com".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);       
  37.            }   
  38.            / (责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容