網站文章自動增加站內鏈接這個我想大多數據同學的網站都有這個功能,下面小編總結了三種文章內鏈自動增加的功能,從上往下,最好的方法在最上面。
例1,代碼如下:
- /**
- *對內容中的關鍵詞添加鏈接
- *只處理第一次出現的關鍵詞,對已有鏈接的關鍵不會再加鏈接,支持中英文
- *$content:string 原字符串
- *$keyword:string 關鍵詞
- *$link:string,鏈接
- */
- 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;
- /**
- * Generate view spots keywords link
- *
- * @param string $description
- * @param array $spotUrlMap
- * @return string
- */
- 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>";
- // 描述里面只有文字,沒有圖片,所以只要注意 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;
- }
- /**
- * replace key word for one time
- *
- * @param string $needle
- * @param string $replace
- * @param string $haystack
- * @param bool $is_replaced
- * @return string
- */
- 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){
- //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://m.survivalescaperooms.com".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);
- }
- //}
- }
- echo $str;
- ?>
新聞熱點
疑難解答