Home >Backend Development >PHP Tutorial > php pre_replace() 高亮展示文字
php pre_replace() 高亮显示文字
希望在下面的文字当中高亮显示单词in
in the rooming, he got into the room, when he's ordered an inexpensive.
我是这样写的,但是连into,inexpensive, rooming,中的in 都高亮显示了;而且空格都没有了。
$pttn = "$newrow";
$str = preg_replace("/\s($newrow)\s/i",$pttn,$str);
如何写才能只显示in,而不会吧into,inexpensive, rooming,中的in也高亮??
$str = "in the rooming, he got into the room, when he's ordered an inexpensive.";<br /> <br /> $newrow = 'in';<br /> $pttn = "<span style='color:red'>$newrow</span>";<br /> <br /> echo $str = preg_replace("/\b($newrow)\b/i",$pttn, $str);<br />
<br /> <br /> $str = "in the rooming, he got into the room, when he's ordered an inexpensive. \s";<br /> <br /> $newrow = '\s'; // 这个会换效。<br /> $pttn = "<span style='color:red'>$newrow</span>";<br /> <br /> echo $str = preg_replace("/\b($newrow)\b/i",$pttn, $str);<br />
<br /> <br /> $str = "\s in the rooming, he got into \s the room, when he's ordered an inexpensive. \s";<br /> $newrow = "\s";<br /> $newrow2 = addslashes($newrow);<br /> $pttn = "<span style='color:red'>$newrow</span>";<br /> //有一个细节,我也没搞明白 ,这里用 \b 失效了,先把结果输出来。<br /> echo $str = preg_replace("/(\s+<br><font color='#FF8000'>------解决方案--------------------</font><br>^)($newrow2)(\s+<br><font color='#FF8000'>------解决方案--------------------</font><br>$)/i","\\1".$pttn."\\3", $str); <br />