I need to highlight a certain characters from a string, I tried str_replace and preg_replace. But these work only if full word is entered,
$text = str_replace($searhPhrase, '<b>'.$searhPhrase.'</b>', $text);
$text = preg_replace('/('. $searhPhrase .')/i', '<b>$1</b>', $text);
I want something like, if I search for 'and' even the letters from 'hand' should get highlighted.
Thanks in advance.
$text = preg_replace('/\S*('. $searhPhrase .')\S*/i', '<b>$1</b>', $text);
This should do it for you.
or
if you want to highlight the whole word
$text = preg_replace('/(\S*'. $searhPhrase .'\S*)/i', '<b>$1</b>', $text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With