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

使用织梦系统的文档关键词维护,假如增加两个关键词为“织梦”和“织梦先生”,那么在文章中出现“织梦先生”这个词的时候,锚文本HTML就会出错,还有php版本高于5.5也会出错;
需要修改两个函数,都在同一个文件中(include/arc.archives.class.php),一个是类Archives中的ReplaceKeyword()函数,另一个是文件最末尾的_highlight()函数。

 /**
     * 高亮问题修正, 排除alt title 直接的字符替换
     *
     * @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>)#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[] = "$key"; // 删除  ,增加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."
"; //恢复超链接 $body = preg_replace("/()/isU", '\\1>\\3<\\4', $body); //暂时屏蔽超链接 $body = preg_replace("/()(.*)(<)(\/a>)/isU", '\\1-]-\\4-[-\\6', $body); } //恢复超链接 $body = preg_replace("#()#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;
}

你可能感兴趣的:(织梦)