Make certain characters of a word from a string bold

I need to highlight certain characters from a string, I tried str_replace and preg_replace. But they only work when you enter a full word,

$text = str_replace($searhPhrase, '<b>'.$searhPhrase.'</b>',  $text);
$text = preg_replace('/('. $searhPhrase .')/i', '<b>$1</b>', $text);

      

I need something like, if I'm looking for "and", even the letters from "hand" should be selected.

Thanks in advance.

+3


source to share


1 answer


$text = preg_replace('/\S*('. $searhPhrase .')\S*/i', '<b>$1</b>', $text);

      

This should do it for you.

or



if you want to select the whole word

$text = preg_replace('/(\S*'. $searhPhrase .'\S*)/i', '<b>$1</b>', $text);

      

+5


source







All Articles