我需要突出显示字符串中的某些字符, 我尝试了 str_replace 和 preg_replace。但是这些只有在输入完整的单词时才有效,
$text = str_replace($searhPhrase, '<b>'.$searhPhrase.'</b>', $text);
$text = preg_replace('/('. $searhPhrase .')/i', '<b>$1</b>', $text);
我想要类似的东西,如果我搜索“and”,即使是“hand”中的字母也应该突出显示。
提前致谢。
最佳答案
$text = preg_replace('/\S*('. $searhPhrase .')\S*/i', '<b>$1</b>', $text);
这应该为您完成。
或
如果你想突出整个单词
$text = preg_replace('/(\S*'. $searhPhrase .'\S*)/i', '<b>$1</b>', $text);
关于php - 将字符串中某个单词的某些字符设为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31606793/