【发布时间】:2021-08-14 14:06:23
【问题描述】:
我有一个未解决的案件。我需要突出显示 $sentence 中的一些 $search 文本。
$result=preg_replace("/\p{L}*?".preg_quote($search)."\p{L}*/ui", "<strong style='color:yellow'>$0</strong>", $sentence);
这个函数做得很好,找到子字符串的每一个出现,但它不仅突出显示(替换)包含子字符串的单词部分,还突出显示(替换)整个单词(从 whitespase 到空白)。
例如:
$sentence='This is the sentence to be searhed through';
$search = 'sent';
echo $result gives "This is the **sentence** to be searhed through"
但我需要
“这是发送要被搜索的内容”
有人可以帮助我理解我做错了什么吗?提前致谢。
【问题讨论】:
标签: php regex preg-replace