使用dedecmsCMS系统的文档关键词维护,假如增加两个关键词为“dedecmsCMS”和“dedecmsCMS先生”,那么在文章中出现“dedecmsCMS先生”这个词的时候,锚文本HTML就会出错,我想这是很多用过这个功能的SEOer见到过的。
在很早以前我就发现过,但是因为自己已经很长时间没有使用dedecmsCMS系统了,只是这次帮助客户修改时,有这个需求,就在这里做一下记录。
需要修改两个函数,都在同一个文件中(include/arc.archives.class.php),一个是类Archives中的ReplaceKeyword()函数,另一个是文件最末尾的_highlight()函数。
修改源码中把dedecmsCMS原版代码注释掉了,以便比较源码和理解修改思路,源码如下:
01 /**02 * 高亮问题修正, 排除alt title <a></a>直接的字符替换03 *04 * @param string $kw05 * @param string $body06 * @return string07 */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();1516 //暂时屏蔽超链接17 $body = preg_replace("#(<a(.*))(>)(.*)(<)(/a>)#isU", '\\1-]-\\4-[-\\6', $body);1819 // $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 }3031 // 这里可能会有错误32 // $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('\\2', $karr, $kaarr, '\\1')", $body);3334foreach ($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}4344 //恢复超链接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 else26 {27 $string = str_replace($words, $result, $string);28 }29 return $pre.$string;30}THE END








