Try this:
preg_replace("/w*?$keywordw*/i", "<b>$0</b>", $str)
w*?
matches any word characters before the keyword (as least as possible) and w*
any word characters after the keyword.
And I recommend you to use preg_quote
to escape the keyword:
preg_replace("/w*?".preg_quote($keyword)."w*/i", "<b>$0</b>", $str)
For Unicode support, use the u flag and p{L}
instead of w
:
preg_replace("/p{L}*?".preg_quote($keyword)."p{L}*/ui", "<b>$0</b>", $str)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…