一对一免费咨询: 13360705693

PHP用正则加亮关键字

日期:2013-06-26  阅读:2616

PHP用正则加亮关键字

<?
function highlight_words($str, $words, $color = '#FFFF00') {  
if(
is_array($words)) {  
   foreach(
$words as $k => $word) {  
     
$pattern[$k] = "/\b($word)\b/is";  
     
$replace[$k] = '<font style="background-color:'.$color.';">\\1</font>';  
   }  
}  
else {  
   
$pattern = "/\b($words)\b/is";  
   
$replace = '<font style="background-color:'.$color.';">\\1</font>';  
}  
   
return
preg_replace($pattern,$replace,$str);  
}  

?>

示例:

<?

$str
="sdfasdfasdfas AS asds";
$word="as";
echo
highlight_words($str,$word);
?>

 

0
相关资讯relevant news
找网站建设公司就找k工作室!
133 6070 5693
tel133 6070 5693