百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>织梦CMS教程> 织梦文档要害词保护中设置词语重叠后出错的修改办法
分享文章到:

织梦文档要害词保护中设置词语重叠后出错的修改办法

发布时间:09/08 来源:未知 浏览: 关键词:

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

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

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

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

 

01  /**
02     * 高亮问题修正, 排除alt title <a></a>直接的字符替换
03     *
04     * @param string $kw
05     * @param string $body
06     * @return string
07     */
08    function ReplaceKeyword($kw,&$body)
09    {
10        global $cfg_cmspath;
11        $maxkey = 5;
12        $kws = explode(",",trim($kw));    //以分好为间隔符
13        $i=0;
14        $karr = $kaarr = $GLOBALS['replaced'] = array();
15 
16        //暂时屏蔽超链接
17        $body = preg_replace("#(<a(.*))(>)(.*)(<)(/a>)#isU"'\1-]-\4-[-\6', $body);
18 
19        // $query = "SELECT * FROM keywords WHERE rpurl<>'' ORDER BY rank DESC"; // 原版的
20$query="SELECT * FROM `keywords` WHERE rpurl<>'' and sta=1 ORDER BY length(keyword) DESC"; // 修改 优先处理长关键词
21        $this->dsql->SetQuery($query);
22        $this->dsql->Execute();
23        while($row = $this->dsql->GetArray())
24        {
25            $key = trim($row['keyword']);
26            $key_url=trim($row['rpurl']);
27            $karr[] = $key;
28            $kaarr[] = "<a class='title-prefix' href='$key_url' target='_blank'>$key</a>"; // 删除 <u> 和 </u> ,增加class属性
29        }
30 
31        // 这里可能会有错误
32        // $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe""_highlight('\2', $karr, $kaarr, '\1')", $body);
33 
34foreach ($karr as $key => $word)
35
36$body = preg_replace("/(^|>)([^<]+)(?=<|$)/sUe""_highlight('\2', $karr[$key], $kaarr[$key], '\1')", $body); 
37//echo $body."<br/>"
38//恢复超链接 
39$body = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU"'\1>\3<\4', $body); 
40//暂时屏蔽超链接 
41$body = preg_replace("/(<a(.*))(>)(.*)(<)(/a>)/isU"'\1-]-\4-[-\6', $body); 
42}
43 
44        //恢复超链接
45        $body = preg_replace("#(<a(.*))-]-(.*)-[-(/a>)#isU"'\1>\3<\4', $body);
46        return $body;
47    }

01//高亮专用, 替换多次是可能不能达到最多次
02function _highlight($string, $words, $result, $pre)
03{
04    global $cfg_replace_num;
05    $string = str_replace('"''"', $string);
06if($GLOBALS['replaced'][$words] == 1){
07return $pre.$string;
08}
09    if($cfg_replace_num > 0)
10    {
11        // foreach ($words as $key => $word)
12        // {
13        //     if($GLOBALS['replaced'][$word] == 1)
14        //     {
15        //         continue;
16        //     }
17            //$string = preg_replace("#".preg_quote($key)."#", $result[$key], $string, $cfg_replace_num);
18            $string = preg_replace("#".preg_quote($words)."#", $result, $string, $cfg_replace_num);  // 修改后
19            if(strpos($string, $words) !== FALSE)
20            {
21                $GLOBALS['replaced'][$words] = 1;
22            }
23        // }
24    }
25    else
26    {
27        $string = str_replace($words, $result, $string);
28    }
29    return $pre.$string;
30}

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有154人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板