Try this:
preg_replace("/\w*?$keyword\w*/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)