3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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])) { $str = '<span class="fc-red">'.$termKey[$i].'</span>'; $newTitle = preg_replace("/$termKey[$i]/i" , $str , $newTitle,1); } } } echo $newTitle;
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
<span class="fc-red"><span class="fc-red">服务</span>员</span>