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

http://php.net/manual/en/function.preg-replace.php


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -