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,代碼如下:
-
-
-
-
-
-
-
- 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,這個(gè)是自己最初學(xué)php時(shí)寫(xiě)的,感覺(jué)有點(diǎn)問(wèn)題,代碼如下:
- <?php
- $keys =array(
- array('網(wǎng)頁(yè)特效','/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('網(wǎng)絡(luò)','/mon/mon.html'),
- array('css','/cssdiv/css.html'),
- array('平面設(shè)計(jì)','/fw/photo.html'),
- array('網(wǎng)站','/person/'),
- array('網(wǎng)頁(yè)制作','/wy/yw.html'),
- array('搜索引擎','/seo/seo.html'),
- array('優(yōu)化','/seo/seo.html'),
- array('動(dòng)畫(huà)','/flash_a/flash.html'),
- array('數(shù)據(jù)庫(kù)','/database/database.html'),
- array('掙錢','/mon/mon.html'),
- array('運(yùn)營(yíng)','/mon/mon.html')
-
- );
-
- $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è)素材";
-
-
- echo $str,"<br>";
- foreach($keys as $nkeys){
-
-
-
- if(strpos($str,$nkeys[0]) ){
- $str =str_replace($nkeys[0],"<a href=http://www.phpfensi.com".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);
- }
-
- }
-
- echo $str;
- ?>
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)