PHP5.5以上DEDECMS自动关键词链接失效的处理方式

PHP5.5版本以下,DEDECMS的自动关键词功能可用,但是过了5.5版本,自动关键词功能失效。

DEDECMS官方当时就留了一句话

// 这里可能会有错误
        if (version_compare(PHP_VERSION, '5.5.0', '>='))
        {
            $body = @preg_replace_callback("#(^|>)([^<]+)(?=<|$)#sU", "_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
        } else {
            $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
        }

然后就没有然后了,我们有时候版本不一定就使用5.4版本,那么高于5.5版本自动关键词功能如何修复呢?

PHP5.5以上DEDECMS自动关键词链接失效的处理步骤

打开/include/arc.archives.class.php

找到

$query = "SELECT * FROM bus_keywords WHERE rpurl<>'' ORDER BY rank DESC";

从这句话开始替换

        $query = "SELECT * FROM bus_keywords WHERE rpurl<>'' ORDER BY rank DESC";
        $this->dsql->SetQuery($query);
        $this->dsql->Execute();
        while($row = $this->dsql->GetArray())
        {
            global $cfg_replace_num;
            $key = trim($row['keyword']);
            $key_url=trim($row['rpurl']);
            $karr[] = $key;
            $kaarr[] = "<a href="'$lfz_vsm'" target='_blank'><u>$key</u></a>";
            //$body = str_replace_limit($key, "<a href="'$lfz_vsm'" target='_blank'><u>$key</u></a>", $body, $cfg_replace_num);
        }

        // 这里可能会有错误
        if (version_compare(PHP_VERSION, '5.5.0', '>='))
        {
            //$body = @preg_replace_callback("#(^|>)([^<]+)(?=<|$)#sU", "_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
            if($cfg_replace_num > 0)
            {
                $query = "SELECT * FROM bus_keywords WHERE rpurl<>'' ORDER BY rank DESC";
                $this->dsql->SetQuery($query);
                $this->dsql->Execute();
                while($row = $this->dsql->GetArray())
                {
                    $key = trim($row['keyword']);
                    $key_url=trim($row['rpurl']);
                    $body = str_replace_limit($key, "<a href="'$lfz_vsm'" target='_blank'><u>$key</u></a>", $body, $cfg_replace_num);
                }
               
            }
            else
            {
                $query = "SELECT * FROM bus_keywords WHERE rpurl<>'' ORDER BY rank DESC";
                $this->dsql->SetQuery($query);
                $this->dsql->Execute();
                while($row = $this->dsql->GetArray())
                {
                    $key = trim($row['keyword']);
                    $key_url=trim($row['rpurl']);
                    $body = str_replace($key, "<a href="'$lfz_vsm'" target='_blank'><u>$key</u></a>", $body);
                }
            }

        } else {
            $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
        }

        //恢复超链接
        $body = preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU", '\\1>\\3<\\4', $body);
        return $body;
    }


}//End Archives

//指定替换次数功能
function str_replace_limit($search, $replace, $subject, $limit){
  if(is_array($search)){
    foreach($search as $k=>$v){
      $search[$k] = '`'. preg_quote($search[$k], '`'). '`';
    }
  }else{
    $search = '`'. preg_quote($search, '`'). '`';
  }
  return preg_replace($search, $replace, $subject, $limit);
}

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

完成以上替换后,DEDECMS在PHP5.5版本以上自动关键词功能就修复完成了。当然你后台需要开启自动关连和替换次数的设置。

标题:PHP5.5以上DEDECMS自动关键词链接失效的处理方式

如若转载,请注明出处:https://www.bus81.com/course/32.html