教程 给朋友做站过程中,需要关键字内链,但是由于PHPCMS关键字关联链接替换的时候 对ALT标签也会替换掉 还有A标签内的东西也会替换掉 这样不是我们的本意,搜遍论坛没有解决方案,看到一个网站解决了,问了下那个朋友,人家告诉我1000元,说官方都没有解决的问题 你说值不值1000,我就纳闷了,自己研究了下DEDE和帝国的程序,搞明白了怎么回事,我php不太好,马马虎虎吧
现在将研究的方法分享出来,供大家使用,找到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); //恢复超链接 liehuo.net $txt = preg_replace("/(<a(.*))-]-(.*)-[-(/a>)/isU", '1>3<<A href="file://4'">4', $txt); return $txt; } //高亮专用, 替换多次是可能不能达到最多次 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; } |