[英]How to highlight multiple keywords in a mouthful in PHP?
Suppose there are 3 keywords, 假设有3个关键字,
I don't want to do this kind of replace 3 times: 我不想进行3次这种替换:
a => <b>a</b>
str_replace ( 'a', '<b>a</b>', $str)
Is it possible to do it by one run? 可以一次运行吗?
You can use the strtr (or mb_strtr) function in PHP 您可以在PHP中使用strtr(或mb_strtr)函数
$trans = array("hello" => "hi", "said" => "screamed");
echo strtr("hi all, I said hello", $trans); // prints out "hi all, I screamed hi"
Use preg_replace()
with a backreference: 结合使用preg_replace()
和反向引用:
$text = "foo bar baz";
echo preg_replace('/(a)/', '<b>$1</b>', $text);
preg_replace('/(a|e|i|o|u)/', '<b>$1</b>', $string);
您是否考虑过研究<b>
与<strong>
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.