Highlight terms in WordPress search results

I was looking ways to highlight search results in search.php and found this easy solution. I’ve modified it a bit to highlight the post’s excerpt as well.

<?php // highlight the search results
    $title = get_the_title();
    $excerpt = get_the_excerpt();

    $keys= explode(" ",$s);

    $title = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="highlight">\0</span>', $title);
    $excerpt = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="highlight">\0</span>', $excerpt);
?>

To display your search title and excerpt, simply print them like this:

<?php echo $title; ?>
<?php echo $excerpt; ?>

Super-simple and it works!