- strstr: documentation ( source)
- strip_tags: documentation ( source)
- preg_replace: documentation ( source)
<?php
$title="服务员";
$termKey=array("服务","服务员");
if($title && !empty($termKey) && is_array($termKey)){
$newTitle = $title;
$cur = count($termKey) - 1; //倒叙匹配
for($i=$cur;$i >= 0 ;--$i) {
if($termKey[$i] && strstr($newTitle , $termKey[$i])) {
$termKey[$i]=strip_tags($termKey[$i]);
$str = '<span class="fc-red">'.$termKey[$i].'</span>';
$newTitle = preg_replace("/$termKey[$i]/i" , $str , $newTitle,1);
}
}
}
echo $newTitle;