3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $title = "服务员服务员服务服务员服务员"; $term = "服务,服务员"; $termKey = explode(',' , $term); $tempTitle = ''; $strlength = strlen($title); 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])) { $position = mb_strrpos($newTitle,'</span>'); if($position){ $tempTitle .= mb_substr($newTitle , 0 , $position+7 , 'UTF-8'); echo $tempTitle;die; } $str = '<span class="fc-red">'.$termKey[$i].'</span>'; $jieduan=mb_substr($newTitle , $position , $strlength , 'UTF-8'); echo '---'.$jieduan.'----'; $newTitle = preg_replace("/$termKey[$i]/i" , $str , $jieduan ,1); } } $newTitle = $tempTitle.$newTitle; echo $newTitle; }else echo $title;
based on KKvC8
Output for 5.6.8 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
---服务员服务员服务服务员服务员----<span class="fc-red">服务员</span>
Output for 4.3.0 - 5.5.35
---服务员服务员服务服务员服务员----<span class="fc-red">服务员</span>服务员服务服