多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > php教程 > php實(shí)現(xiàn)文章內(nèi)容關(guān)鍵字增加內(nèi)鏈

php實(shí)現(xiàn)文章內(nèi)容關(guān)鍵字增加內(nèi)鏈

來(lái)源:程序員人生   發(fā)布時(shí)間:2014-04-10 00:25:20 閱讀次數(shù):4691次

網(wǎng)站文章自動(dòng)增加站內(nèi)鏈接這個(gè)我想大多數(shù)據(jù)同學(xué)的網(wǎng)站都有這個(gè)功能,下面小編總結(jié)了三種文章內(nèi)鏈自動(dòng)增加的功能,從上往下,最好的方法在最上面。

例1,代碼如下:

  1. /**  
  2. *對(duì)內(nèi)容中的關(guān)鍵詞添加鏈接  
  3. *只處理第一次出現(xiàn)的關(guān)鍵詞,對(duì)已有鏈接的關(guān)鍵不會(huì)再加鏈接,支持中英文  
  4. *$content:string 原字符串  
  5. *$keyword:string  關(guān)鍵詞  
  6. *$link:string,鏈接  
  7. */  
  8. function yang_keyword_link($content,$keyword,$link){  
  9.     //排除圖片中的關(guān)鍵詞  
  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.     //還原圖片中的關(guān)鍵詞  
  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.         // 排除不規(guī)則數(shù)據(jù) 
  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.             // 描述里面只有文字,沒(méi)有圖片,所以只要注意 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,這個(gè)是自己最初學(xué)php時(shí)寫(xiě)的,感覺(jué)有點(diǎn)問(wèn)題,代碼如下:

  1. <?php   
  2.     $keys =array(   
  3.          array('網(wǎng)頁(yè)特效','/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('網(wǎng)絡(luò)','/mon/mon.html'),   
  14.          array('css','/cssdiv/css.html'),   
  15.          array('平面設(shè)計(jì)','/fw/photo.html'),   
  16.          array('網(wǎng)站','/person/'),   
  17.          array('網(wǎng)頁(yè)制作','/wy/yw.html'),   
  18.          array('搜索引擎','/seo/seo.html'),   
  19.          array('優(yōu)化','/seo/seo.html'),   
  20.          array('動(dòng)畫(huà)','/flash_a/flash.html'),   
  21.          array('數(shù)據(jù)庫(kù)','/database/database.html'),   
  22.          array('掙錢','/mon/mon.html'),   
  23.          array('運(yùn)營(yíng)','/mon/mon.html')   
  24.              
  25.     );   
  26.            
  27.     $str ="今天是2010年5月30號(hào),我的網(wǎng)站出現(xiàn)的問(wèn)題這對(duì)seo有很多的問(wèn)題,seo就是搜索引擎優(yōu)化了,以前學(xué)php好啊現(xiàn)在覺(jué)得jsp好,css+div,網(wǎng)頁(yè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)頁(yè)制作,網(wǎng)頁(yè)學(xué)習(xí),網(wǎng)頁(yè)教學(xué),Photoshop,Flash,HTML,CSS,Dreamweaver,Fireworks,ASP,PHP,JSP,ASP.NET,網(wǎng)站建設(shè),網(wǎng)站開(kāi)發(fā),網(wǎng)頁(yè)特效,平面設(shè)計(jì),個(gè)人網(wǎng)站,網(wǎng)頁(yè)素材";   
  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.phpfensi.com".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);       
  37.            }   
  38.            //}   
  39.     }   
  40.            
  41.        echo $str;   
  42.     ?>
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 久草综合在线 | 国产欧美精品一区二区三区四区 | 国产成人91 | 国产日产欧美一区二区三区 | 国产成人啪午夜精品网站男同 | 色老头福影院韩国激情影院 | 色视频2| 欧美性受xxxx黑人 | 一区二区三区不卡在线观看 | 亚洲欧洲日本天天堂在线观看 | 操操网址 | 国产日韩一区二区 | 自由成熟的性色视频 | 久久99爱爱 | 国产福利片在线 易阳 | 亚洲不卡在线观看 | 日本护士xxxjapanese | 成人在线网 | 久操影视| 国产精品成aⅴ人片在线观看 | 日本网络视频www色高清免费 | 就操网| 欧美性生交大片 | 日本免费一区二区三区视频 | 中文字幕在线免费观看视频 | 在线观看av网站永久 | 性欧美video另类hd尤物 | 校园 图片区 视频 小说专区 | 一二三四视频社区5在线高清视频 | 亚洲视频在线观看视频 | 国产v日韩v欧美v精品专区 | 一二三四视频中文字幕在线看 | 亚洲品质自拍视频网站 | 老司机午夜视频在线观看 | 亚洲不卡 | 国产日韩一区在线精品欧美玲 | 一本大道香蕉高清久久 | 一区二区三区在线视频播放 | 1024在线视频国产在线播放 | 亚洲 欧美 精品 中文第三 | 国产第一页福利 |