php文章内容替换为内链,有助于SEO优化


/**
 * 文章内容替换为内链
 * @param $content
 * @return mixed
 */
function get_glo_keywords($content)
{
    /* 这里可以改为读取数据表,然后缓存起来 */
    $keywords_list = array(
        ['title'=>'小程序', 'url'=>'http://www.xin-shidai.com'],
        ['title'=>'导航网址', 'url'=>'http://www.xin-shidai.com'],
    );
    if($keywords_list){
        $readnum = 0;
        foreach ($keywords_list as $key => $val) {
            $title = $val['title'];
            $len = strlen($title);
            $str = '<a href="'.$val['url'].'" target="_blank">'.$title.'</a>';
            $str_index = mb_strpos($content, $title);
            $content = preg_replace('/(?!<[^>]*)'.$title.'(?![^<]*>)/', $str, $content, 1);
            if(is_numeric($str_index)){
                $readnum += 1;
                //$content = substr_replace($content,$str,$str_index,$len);
                //$content = $this->str_replace_limit($title,$str,$content,$this->limit);
            }
            if($readnum == 8) return $content; //匹配到8个关键词就退出
        }
    }
    return $content;
}

猜你喜欢

转载自blog.csdn.net/xiao_hu520/article/details/79469085