typecho如何替换文章内容的友链

2024年08月17日 建站教程

为了有助于网站seo,typecho如何直接替换文章内容的友链,直接附代码:

$Keywordspress = new Typecho_Widget_Helper_Form_Element_Textarea('Keywordspress', NULL, NULL, _t('关键字内链'), _t('每行1组以"关键词|(半角竖线)链接"形式填写)'));
    $form->addInput($Keywordspress);

/**
 * 文章内容替换为内链
 */
function get_glo_keywords($content){
    $settings = Helper::options()->Keywordspress;    
    $keywords_list = array();
    
    if (strpos($settings,'|')) {
            //解析关键词数组
            $kwsets = array_filter(preg_split("/(\r|\n|\r\n)/",$settings));
            foreach ($kwsets as $kwset) {
                $keywords_list[] = explode('|',$kwset);
            }
        }
    ksort($keywords_list);  //对关键词排序,短词排在前面
    
    if($keywords_list){
        $readnum = 0;
        $i = 0;
        $j = 1;
        foreach ($keywords_list as $key => $val) {
            
            $title = $val[$i];
            $len = strlen($title);
            $str = '<a href="'.$val[$j].'" 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个关键词就退出
            $i += 2;
            $j += 2;
            }
        }
    }
    return $content;
}

本文链接:http://so.lmcjl.com/news/11057/

展开阅读全文