dedecms關鍵字不需要在keyword中設置實現(xiàn)內(nèi)鏈
來源:
www.51fangzhan.com作者:51仿站更新時間:2012-04-09 17:23點擊: 369
dedecms關鍵詞內(nèi)連維護默認是需要和發(fā)布文章時匹配通過keyword中設置的關鍵字作為條件替換來完成的,今天分享的是不通過keyword中設置的關鍵字作為條件替換,而是在數(shù)據(jù)庫中所有的詞中索引匹配的
織夢仿站方法:
測試環(huán)境為:DEDECMS v5.7
第一步:/include/arc.archives.class.php(DEDE v5.7) line 231:if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y' && !empty($this->Fields['keywords']))
改為:if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y')
第二步:/include/arc.archives.class.php(DEDE v5.7)中的 ReplaceKeyword 方法改成:
function ReplaceKeyword($kw,&$body)
{
global $cfg_cmspath;
$maxkey = 3;
$kws = explode(",",trim($kw)); //以分好為間隔符
$i=0;
$karr = $kaarr = $GLOBALS['replaced'] = array();
//暫時屏蔽超鏈接
$body = preg_replace("#(<a(.*))(>)(.*)(<)(\/a>)#isU", '\\1-]-\\4-[-\\6', $body);
/*
foreach($kws as $k)
{
$k = trim($k);
if($k!="")
{
if($i > $maxkey)
{
break;
}
$myrow = $this->dsql->GetOne("SELECT * FROM cn_keywords WHERE keyword='$k' AND rpurl<>'' ");
if(is_array($myrow))
{
$karr[] = $k;
$GLOBALS['replaced'][$k] = 0;
$kaarr[] = "<a href='{$myrow['rpurl']}'><u>$k</u></a>";
}
$i++;
}
}
*/
$query = "SELECT * FROM cn_keywords WHERE rpurl<>'' ORDER BY rank DESC";
$this->dsql->SetQuery($query);
$this->dsql->Execute();
$j = 0;
while($row = $this->dsql->GetArray())
{
$action = 1;
if($j >= $maxkey) break;
$key = trim($row['keyword']);
$key_url = trim($row['rpurl']);
if(count(explode($key,$body)) > 1 || count(explode($body,$key)) > 1) {
$p = 0;
if(empty($karr)){
$karr[] = $key;
$kaarr[] = "<a href='$key_url' target='_blank'><u>$key</u></a>";
$j++;
}
else{
foreach($karr as $k => $v){
$p++;
if(count(explode($key,$v)) > 1 || count(explode($v,$key)) > 1){
$action = 0;
break;
}
}
if($p == count($karr) && $action){
$karr[] = $key;
$kaarr[] = "<a href='$key_url' target='_blank'><u>$key</u></a>";
$j++;
}
}
}
}
// 這里可能會有錯誤
$body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
//恢復超鏈接
$body = preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU", '\\1>\\3<\\4', $body);
return $body;
}