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,$string); } return $pre.$string; } |