织梦文档关键词维护中设置词语重叠后出错的修改方法

使用织梦系统的文档关键词维护,假如增加两个关键词为“织梦”和“织梦先生”,那么在文章中出现“织梦先生”这个词的时候,锚文本HTML就会出错,我想这是很多用过这个功能的SEOer见到过的。

在很早以前我就发现过,但是因为自己已经很长时间没有使用织梦系统了,只是这次帮助客户修改时,有这个需求,就在这里做一下记录。

需要修改两个函数,都在同一个文件中(include/arc.archives.class.php),一个是类Archives中的ReplaceKeyword()函数,另一个是文件最末尾的_highlight()函数。

修改源码中把织梦原版代码注释掉了,以便比较源码和理解修改思路,源码如下:

 

  /**     * 高亮问题修正, 排除alt title <a></a>直接的字符替换     *     * @param string $kw     * @param string $body     * @return string     */    function ReplaceKeyword($kw,&$body)    {        global $cfg_cmspath;        $maxkey = 5;        $kws = explode(",",trim($kw));    //以分好为间隔符        $i=0;        $karr = $kaarr = $GLOBALS['replaced'] = array();        //暂时屏蔽超链接        $body = preg_replace("#(<a(.*))(>)(.*)(<)(/a>)#isU", '\1-]-\4-[-\6', $body);        // $query = "SELECT * FROM keywords WHERE rpurl<>'' ORDER BY rank DESC"; // 原版的$query="SELECT * FROM `keywords` WHERE rpurl<>'' and sta=1 ORDER BY length(keyword) DESC"; // 修改 优先处理长关键词        $this->dsql->SetQuery($query);        $this->dsql->Execute();        while($row = $this->dsql->GetArray())        {            $key = trim($row['keyword']);            $key_url=trim($row['rpurl']);            $karr[] = $key;            $kaarr[] = "<a class='title-prefix' href='$key_url' target='_blank'>$key</a>"; // 删除 <u> 和 </u> ,增加class属性        }        // 这里可能会有错误        // $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('\2', $karr, $kaarr, '\1')", $body);foreach ($karr as $key => $word){  $body = preg_replace("/(^|>)([^<]+)(?=<|$)/sUe", "_highlight('\2', $karr[$key], $kaarr[$key], '\1')", $body);  //echo $body."<br/>";  //恢复超链接  $body = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU", '\1>\3<\4', $body);  //暂时屏蔽超链接  $body = preg_replace("/(<a(.*))(>)(.*)(<)(/a>)/isU", '\1-]-\4-[-\6', $body);  }        //恢复超链接        $body = preg_replace("#(<a(.*))-]-(.*)-[-(/a>)#isU", '\1>\3<\4', $body);        return $body;    }

//高亮专用, 替换多次是可能不能达到最多次function _highlight($string, $words, $result, $pre){    global $cfg_replace_num;    $string = str_replace('"', '"', $string);if($GLOBALS['replaced'][$words] == 1){return $pre.$string;}    if($cfg_replace_num > 0)    {        // foreach ($words as $key => $word)        // {        //     if($GLOBALS['replaced'][$word] == 1)        //     {        //         continue;        //     }            //$string = preg_replace("#".preg_quote($key)."#", $result[$key], $string, $cfg_replace_num);            $string = preg_replace("#".preg_quote($words)."#", $result, $string, $cfg_replace_num);  // 修改后            if(strpos($string, $words) !== FALSE)            {                $GLOBALS['replaced'][$words] = 1;            }        // }    }    else    {        $string = str_replace($words, $result, $string);    }    return $pre.$string;}

以上就是织梦文档关键词维护中设置词语重叠后出错的修改方法的介绍,希望大家喜欢!

更多教程关注FF推源码网。

提供最优质的资源集合

立即查看 了解详情