dedecms自動(dòng)給文章中的關(guān)鍵字加超鏈接完整版
時(shí)間: 2020-03-30 01:15
閱讀: 次
作者:素材無憂網(wǎng)
織夢(mèng)dedecms自動(dòng)給文章中的關(guān)鍵字加超鏈接完整版 。
在 include/arc.archives.class.php 這個(gè)文件中 有兩個(gè)函數(shù)需要修改 ReplaceKeyword 和 _highlight
把這兩個(gè)函數(shù) 第1014行開始到倒數(shù)第二行結(jié)束 修改為
function ReplaceKeyword($kw,&$body)
{
global $cfg_cmspath;
$maxkey = 5;
$kws = explode(",",trim($kw)); //以分好為間隔符
$i=0;
$karr = $kaarr = $GLOBALS['replaced'] = array();
//暫時(shí)屏蔽超鏈接
$body = preg_replace("/(<a(.*))(>)(.*)(<)(/a>)/isU", '-]--[-', $body);
global $dsql;
$query="SELECT * FROM dede_keywords WHERE rpurl<>'' and sta=1 ORDER BY length(keyword)desc";
$dsql->SetQuery($query);
$dsql->Execute();
while($row = $dsql->GetArray())
{
$key = trim($row['keyword']);
$key_url=trim($row['rpurl']);
$karr[] = $key;
$kaarr[] = "<b><a href='$key_url' target='_blank'>$key</a></b>";
}
foreach ($karr as $key => $word)
{
$body = preg_replace("/(^|>)([^<]+)(?=<|$)/sUe", "_highlight('', $karr[$key], $kaarr[$key], '')", $body);
//echo $body."<br/>";
//恢復(fù)超鏈接
$body = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU", '><', $body);
//暫時(shí)屏蔽超鏈接
$body = preg_replace("/(<a(.*))(>)(.*)(<)(/a>)/isU", '-]--[-', $body);
}
//恢復(fù)超鏈接
$body = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU", '><', $body);
return $body;
}
}//End Archives
//高亮專用, 替換多次是可能不能達(dá)到最多次
function _highlight($string, $words, $result, $pre)
{
global $cfg_replace_num;
$string = str_replace('"', '"', $string);
if($GLOBALS['replaced'][$words] == 1)
{
return $pre.$string;
}
if($cfg_replace_num > 0)
{
$string = preg_replace("/".preg_quote($words)."/", $result, $string, $cfg_replace_num);
if(strpos($string, $words) !== false)
{
$GLOBALS['replaced'][$words] = 1;
//echo $words;
}
}
else
{
$string = str_replace($words, $result, $string);
}
return $pre.$string;
}
保存替換就可以了。
標(biāo)簽: