dedecms关键词维护里面字数多的词优先字数少的词的解决办法

织梦后台的关键词维护默认的情况是字数少的词优先于字数多的词,比如我们有两个这样的词:创业、创业方向,第二个词包含了第一个词,在文章中如果出现“创业方向”这个词,默认情况下只会给创业两个字添加关键词超链接,而不是整个词,那么我们怎么样才能实现字数多的词优先于字数少的词呢?下面就告诉大家具体的解决方法:


我们找到并打开/include/arc.archives.class.php文件,在里面找到如下代码:

01 function ReplaceKeyword($kw,&$body)
02     {
03         global $cfg_cmspath;
04         $maxkey = 5;
05         $kws = explode(",",trim($kw));    //以分好为间隔符
06         $i=0;
07         $karr = $kaarr = $GLOBALS['replaced'] = array();
08  
09         //暂时屏蔽超链接
10         $body = preg_replace("#(<a(.*))(>)(.*)(<)(\/a>)#isU"'\\1-]-\\4-[-\\6', $body);
11  
12         /*
13         foreach($kws as $k)
14         {
15             $k = trim($k);
16             if($k!="")
17             {
18                 if($i > $maxkey)
19                 {
20                     break;
21                 }
22                 $myrow = $this->dsql->GetOne("SELECT * FROM #【分隔符】@__keywords WHERE keyword='$k' AND rpurl<>'' ");
23                 if(is_array($myrow))
24                 {
25                     $karr[] = $k;
26                     $GLOBALS['replaced'][$k] = 0;
27                     $kaarr[] = "<a href='{$myrow['rpurl']}'><u>$k</u></a>";
28                 }
29                 $i++;
30             }
31         }
32         */
33         $query = "SELECT * FROM #【分隔符】@__keywords WHERE rpurl<>'' ORDER BY rank DESC";
34         $this->dsql->SetQuery($query);
35         $this->dsql->Execute();
36         while($row = $this->dsql->GetArray())
37         {
38             $key = trim($row['keyword']);
39             $key_url=trim($row['rpurl']);
40             $karr[] = $key;
41             $kaarr[] = "<a href='$key_url' target='_blank'><u>$key</u></a>";
42         }
43  
44         // 这里可能会有错误
45         $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe","_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
46  
47         //恢复超链接
48         $body = preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU",'\\1>\\3<\\4', $body);
49         return $body;
50     }

注意查找代码的时候先手动去掉代码里面的【分隔符】字样!

找到后将其替换为如下代码:

01     function ReplaceKeyword($kw,&$body)
02     {
03     global $cfg_cmspath,$dsql;
04     $maxkey = 5;
05     $kws = explode(",",trim($kw)); //以分好为间隔符
06     $i=0;
07     $karr = $kaarr = $GLOBALS['replaced'] = array();
08     //暂时屏蔽超链接
09     $body = preg_replace("/(<a(.*))(>)(.*)(<)(\/a>)/isU"'\\1-]-\\4-[-\\6', $body);
10     $query="SELECT * FROM #【分隔符】@__keywords WHERE rpurl<>'' and sta=1 ORDER BY length(keyword)desc";
11     $dsql->SetQuery($query);
12     $dsql->Execute();
13     while($row = $dsql->GetArray())
14     {
15     $key = trim($row['keyword']);
16     $key_url=trim($row['rpurl']);
17     $karr[] = $key;
18     $kaarr[] = "<u><a href='$key_url' target='_blank'>$key</a></u>";
19     }
20     foreach ($karr as $key => $word)
21     {
22     $body = preg_replace("/(^|>)([^<]+)(?=<|$)/sUe", "_highlight('\\2', \$karr[$key], \$kaarr[$key],
23  
24  
25 '\\1')", $body);
26     //echo $body."<br/>";
27     //恢复超链接
28     $body = preg_replace("/(<a(.*))-\]-(.*)-\[-(\/a>)/isU"'\\1>\\3<\\4', $body);//暂时屏蔽超链接
29     $body = preg_replace("/(<a(.*))(>)(.*)(<)(\/a>)/isU"'\\1-]-\\4-[-\\6', $body);
30     }
31     //恢复超链接
32     $body = preg_replace("/(<a(.*))-\]-(.*)-\[-(\/a>)/isU"'\\1>\\3<\\4', $body);
33  
34  
35     return $body;
36     }
37     }
38     //End Archives
39     //高亮专用, 替换多次是可能不能达到最多次
40     function _highlight($string, $words, $result, $pre)
41     {
42     global $cfg_replace_num;
43     $string = str_replace('\"''"', $string);
44 if($GLOBALS['replaced'][$words] == 1){
45     return $pre.$string;
46     }
47 if($cfg_replace_num > 0){
48     $string = preg_replace("/".preg_quote($words)."/", $result, $string, $cfg_replace_num);
49     if(strpos($string, $words) !== false)
50     {
51         $GLOBALS['replaced'][$words] = 1;
52         }
53         }
54 else{$string = str_replace($words, $result, $string);
55 }
56 return $pre.$string;
57 }

注意手动去掉代码里面的【分隔符】字样!

 


替换完成后保存即可,然后生成一下文章内容试试。


AiteCms,企业模板快速建站专家,点击了解更多

 

转载自织梦模板教程(www.dede58.com

posted @ 2017-07-05 10:29  织梦58  阅读(780)  评论(0编辑  收藏  举报