0712-2888027 189-8648-0214
微信公眾號

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號

當(dāng)前位置:主頁 > 技術(shù)支持 > PHPCMS > phpcms v9關(guān)鍵字,內(nèi)聯(lián),關(guān)聯(lián)鏈接完美解決方案

phpcms v9關(guān)鍵字,內(nèi)聯(lián),關(guān)聯(lián)鏈接完美解決方案

時間:2024-09-19來源:風(fēng)信官網(wǎng) 點擊: 584次

由于PHPCMS關(guān)鍵字關(guān)聯(lián)鏈接替換的時候 對ALT標(biāo)簽也會替換掉  還有A標(biāo)簽內(nèi)的東西也會替換掉 這樣不是我們的本意,搜遍論壇沒有解決方案,看到一個網(wǎng)站解決了,問了下那個朋友,人家告訴我1000元,說官方都沒有解決的問題 你說值不值1000,我就納悶了,自己研究了下DEDE和帝國的程序,搞明白了怎么回事,現(xiàn)在將研究的方法分享出來:

找到include目錄下的global.func.php文件,用編輯軟件打開后,CTRL+F搜索function keylinks

然后將:

function keylinks($txt, $replacenum = '')
{
$linkdatas = cache_read('keylink.php','',1);
if($linkdatas)
{
$word = $replacement = array();
foreach($linkdatas as $v)
{
$word1[] = '/'.preg_quote($v[0], '/').'/';
$word2[] = $v[0];
$replacement[] = '<a href="'.$v[1].'" target="_blank" class="keylink">'.$v[0].'</a>';
}
if($replacenum != '')
{
$txt = preg_replace($word1, $replacement, $txt, $replacenum);
}
else
{
$txt = str_replace($word2, $replacement, $txt);
}
}
return $txt;
}

替換成以下代碼:

function keylinks($txt, $replacenum = '')
{
$linkdatas =
cache_read('keylink.php','',1);

//暫時屏蔽超鏈接
$txt
= preg_replace("/(<a(.*))(>)(.*)(<)(\/a>)/isU", '\\1-]-\\4-[-\\6',
$txt);

$GLOBALS['replaced'] = array();

if($linkdatas)
{
$word = $replacement =
array();
foreach($linkdatas as
$v)
{
$word[] =
$v[0];
$GLOBALS['replaced'][$v[0]] =
0;
$replacement[] = '<a href="'.$v[1].'" target="_blank"
class="keylink">'.$v[0].'</a>';
}
}
$txt
= preg_replace("/(^|>)([^<]+)(?=<|$)/sUe", "_highlight('\\2', \$word,
\$replacement, '\\1',\$replacenum)", $txt);

//恢復(fù)超鏈接
$txt = preg_replace("/(<a(.*))-\]-(.*)-\[-(\/a>)/isU", '\\1>\\3<\\4', $txt);

//高亮專用, 替換多次是可能不能達(dá)到最多次
function _highlight($string, $words, $result,
$pre ,$cfg_replace_num)
{
$string = str_replace('\"', '"',
$string);
if($cfg_replace_num > 0)
{
foreach
($words as $key =>
$word)
{
if($GLOBALS['replaced'][$word] ==
1)
{
continue;
}
$string
= preg_replace("/".preg_quote($word)."/", $result[$key], $string,
$cfg_replace_num);
if(strpos($string, $word) !==
false)
{
$GLOBALS['replaced'][$word]
=
1;
}
}
}
else
{
$string
= str_replace($words, $result, $string);
}
return
$pre.$string;
}

就ok了

現(xiàn)在實現(xiàn)的功能是:
第一就是會替換所有的關(guān)聯(lián)關(guān)鍵字
第二如果替換次數(shù)在模型設(shè)置里面設(shè)置的是大于0的話,每個關(guān)鍵字只替換一次,主要考慮到替換太多 不利于SEO
在生成速度上會慢些,還有就是在后臺加的關(guān)鍵字會全部替換,要是關(guān)鍵字太密集的話,也會不利于搜索引擎優(yōu)化。 實現(xiàn)起來也不難。

要想一個關(guān)鍵字可以使用下面的代碼:

//高亮專用, 替換多次是可能不能達(dá)到最多次
function _highlight($string, $words, $result, $pre ,$cfg_replace_num)
{

$string = str_replace('\"', '"', $string);
if($cfg_replace_num > 0)
{
foreach ($words as $key => $word)
{
if($GLOBALS['replaced'][$word] == $cfg_replace_num)
{
continue;
}
$string = preg_replace("/".preg_quote($word)."/", $result[$key], $string, $cfg_replace_num);
if(strpos($string, $word) !== false)
{
$GLOBALS['replaced'][$word] ++;
}
}
}
else
{
$string = str_replace($words, $result, $string);
}
return $pre.$string;
}

欄目列表
推薦內(nèi)容
熱點內(nèi)容
展開