Home > php教程 > php手册 > php实现文章内容关键字增加内链

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

WBOY
Release: 2016-06-13 09:47:07
Original
2390 people have browsed it

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


例1

 代码如下 复制代码

/**
*对内容中的关键词添加链接
*只处理第一次出现的关键词,对已有链接的关键不会再加链接,支持中英文
*$content:string 原字符串
*$keyword:string  关键词
*$link:string,链接
*/
function yang_keyword_link($content,$keyword,$link){
    //排除图片中的关键词
    $content = preg_replace( '|(php实现文章内容关键字增加内链]*?)('.$keyword.')([^>]*?>)|U', '$1%&&&&&%$3', $content);
    $regEx = '/(?!((<.>]*?)>)|([^>]*?))/si';
    $url=''.$keyword.'';
    $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 = "".$spotUrlPair[0]."";
            // 描述里面只有文字,没有图片,所以只要注意 a 链接
            $tmp1 = explode("             $is_replaced=false;
            foreach ($tmp1 as $key=>$item){
                $tmp2 = explode("",$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("",$tmp2);
                    }
                }else {
                    if (is_string($item) && $is_replaced===false) {
                        $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$item,$is_replaced);
                    }
                }
            }
            $description = implode("         }
        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时写的,感觉有点问题

 代码如下 复制代码
        $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,"
"; 
    foreach($keys as $nkeys){ 
           //print_r($nkeys);echo"
"; 
           //foreach( $nkeys as $join) { 
           //echo($join),"
"; 
           if(strpos($str,$nkeys[0]) ){ 
                 $str =str_replace($nkeys[0],"".$nkeys[0]."",$str);      
           } 
           //} 
    } 
         
       echo $str; 
    ?>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template