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

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

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -