how write the script, which menchion the whole word, if it contain the keyword? example: keyword “fun”, string – the bird is funny, result – the bird is * funny*. i do the following
$str = "my bird is funny";
$keyword = "fun";
$str = preg_replace("/($keyword)/i","<b>$1</b>",$str);
but it menshions only keyword. my bird is funny
Try this:
\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_quoteto escape the keyword:For Unicode support, use the u flag and
\p{L}instead of\w: