Highlight text for search results in PHP

Useful code to highlight text occurences in search results or in a text. How to highlight text in a string with PHP and regular expression? Here is the solution:

// highlight text in a string
function highlight($text_highlight, $text_search) {
$str = preg_replace('#'. preg_quote($text_highlight) .'#i', '<span style="background-color:#FFFF66; color:#FF0000;">\\0</span>', $text_search);
return $str;

Anything to say?

Comments with Facebook or below with WordPress

Sorry, WordPress comments are closed on old posts.