'Make certain characters of a word from string bold
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.
Solution 1:[1]
$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);
Solution 2:[2]
if this still not works try with this Function
function strReplace($searchText,$mainText){
return str_replace($searchText, '<b>'.$searchText.'</b>', $mainText);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | vks |
Solution 2 | Paras Sharma |