PHP头条
热点:

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


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


例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(empty(InnerLink::$spotUrlMap)){
            InnerLink::$spotUrlMap = DBViewSpot::getSpotPare();
        }
        // 排除不规则数据
        if ( empty($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://www.111cn.net".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);      
           } 
           //} 
    } 
         
       echo $str; 
    ?>

www.phpzy.comtrue/php/35686.htmlTechArticlephp实现文章内容关键字增加内链 网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的方法在最上面...

相关文章

    暂无相关文章

PHP之友评论

今天推荐