您好,欢迎访问一九零五行业门户网

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

网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的方法在最上面。
例1
 代码如下 复制代码
/**
*对内容中的关键词添加链接
*只处理第一次出现的关键词,对已有链接的关键不会再加链接,支持中英文
*$content:string 原字符串
*$keyword:string  关键词
*$link:string,链接
*/
function yang_keyword_link($content,$keyword,$link){
    //排除图片中的关键词
    $content = preg_replace( '|(]*?)('.$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时写的,感觉有点问题
 代码如下 复制代码
http://www.bkjia.com/phpjc/633156.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/633156.htmltecharticle网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的...
其它类似信息

推荐信息