php - How to match the whole word when we have a sub-string of it? -
here code:
$txt = 'this text'; $word = 'is'; echo str_replace($word, '<b>'.$word.'</b>', $txt); //=> th<b>is</b> <b>is</b> text
as see, sub-string is
in example above , matches is
part of this
. while need select whole of word. expected result:
//=> <b>this</b> <b>is</b> text
so need check both left , right side of sub-string , match until either first of string ^
or end of string $
or white spage \s
.
how can that?
you can use preg_replace
achieve regex
Comments
Post a Comment