sablog-x 2.0提供了自动为tag标签添加链接的功能,这个函数在include\func\front.func.php中
function highlight_tag($content, $tag) {
global $options;
$tag = trim($tag);
if(preg_match('/<a[^>]+?'.preg_quote($tag).'[^>]+?>/i',$content)) return $content;
if(preg_match('/<img[^>]+?'.preg_quote($tag).'[^>]+?>/i',$content)) return $content;
//有次数的替换
$content = preg_replace('/'.$tag.'/i','<a href="'.gettaglink(urlencode($tag)).'" onclick="tagshow(\''.$tag.'\');return false;">'.htmlspecialchars($tag).'</a>', $content, 1);
/*
替换所有
if(function_exists('eregi_replace')) {
$content = eregi_replace($tag, '<a href="'.$tagurl.'" onclick="tagshow(\''.$tag.'\');return false;" class="tagshow">'.htmlspecialchars($tag).'</a>', $content);
} else {
$content = str_replace($tag, '<a href="'.$tagurl.'" onclick="tagshow(\''.$tag.'\');return false;" class="tagshow">'.htmlspecialchars($tag).'</a>', $content);
}
*/
return $content;
}
这个函数其实是有问题的,<a></a>标签对之间出现tag标签时,仍然会替换成超链接。
比如,我一篇文章有一个标签是"tag",而我的文章中同时有一个超链接<a href="www.lurenfake.com/taglist/">this is a tag</a>,它最终会被转换成<a href="www.lurenfake.com/taglist/">this is a <a href="www.luranfke.com/tag/tag/">tag</a></a>,这样就出现了两个超链接嵌套的情况,显示结果会很奇怪。
对寻找链接的正则表达式进行了修改,同时加上了对<pre>标签的过滤,这样代码中的关键字也不会被替换了,修改后的代码如下
function highlight_tag($content, $tag) {
global $options;
$tag = trim($tag);
if(preg_match('/<pre.+?'.preg_quote($tag).'.+?<\/pre>/i',$content)) return $content;
if(preg_match('/<a.+?'.preg_quote($tag).'.+?<\/a>/i',$content)) return $content;
if(preg_match('/<img.+?'.preg_quote($tag).'.+?>/i',$content)) return $content;
//有次数的替换
$content = preg_replace('/'.$tag.'/i','<a style="color:#ff6055" href="'.gettaglink($tag).'" onclick="tagshow(\''.$tag.'\');return false;">'.htmlspecialchars($tag).'</a>', $content, 1);
return $content;
}