highlight search terms in search results

To highlight search terms in the search results you can use the code below. Additionaly you will need to give search-highlight some special properties in your style sheet.


// highlight search terms
function highlight_results($text){
    if(is_search()){
        $keys = implode('|', explode(' ', get_search_query()));
        $text = preg_replace('/(' . $keys .')/iu', '<span class="search-highlight">\0</span>', $text);
    }
    return $text;
}
add_filter('the_content', 'highlight_results', 80);
add_filter('the_excerpt', 'highlight_results', 80);
add_filter('the_title', 'highlight_results', 80);