Highlights words in a phrase

Sometimes it can be really usefull for a visitor to have the terms he serached for highlighted. The following snippet is using a simple regular expression to find words in a phrase and highlight them.

function highlight($sString, $aWords) {
	if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
		return false;
	}

	$sWords = implode ('|', $aWords);
 	return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);
}

Usage

The function take two parameters: The first is the phrase and the second is an array of words to be highlighted in the phrase.

echo highlight('Highlight the words PHP and snippets in a phrase', array ('php', 'snippets'));

Words aren’t case sensitive.

9 Comments

  1. Sergei Tatarinov
    Posted July 3, 2010 at 10:11 am | Permalink

    Exactly what I need! Where should I add that piece of code? to my functions.php?

    • Posted July 4, 2010 at 9:51 am | Permalink

      If you plan to use it within WordPress, yes.

  2. Oleg
    Posted July 6, 2010 at 11:03 pm | Permalink

    You’ve forgot about closed strong tag

    • Posted July 6, 2010 at 11:11 pm | Permalink

      Sorry. The syntax highlighter ate it :/

  3. Posted July 8, 2010 at 9:58 am | Permalink

    your a legend jean

  4. hoses
    Posted July 26, 2010 at 12:26 pm | Permalink

    How can I highlight string with html on it without highlightin the html, and therefore breaking the html?

  5. Posted July 26, 2010 at 3:27 pm | Permalink

    This function can be greatly improved, it’s longer than necessary, has no color options via parameter and you’re forced to use an array even if you only want to highlight one word.

    Whilst it works perfectly fine, it is impractical so i wrote an improved version which you can see here:
    http://webgadgetfinder.com/archives/283

    Enjoy!

    Regards,
    Dan

  6. Olivier Villemain
    Posted July 27, 2010 at 8:31 am | Permalink

    Escape $sWords with preg_quote will be better :

    return preg_replace ('@\b('.preg_quote($sWords).')\b@si', '$1', $sString);

  7. Massimiliano
    Posted July 27, 2010 at 3:48 pm | Permalink

    So good. Thanks man !

2 Trackbacks

  1. [...] This post was mentioned on Twitter by Jean-Baptiste Jung and others. Jean-Baptiste Jung said: RT @phpsnippets: PHP Snippet: Highlights words in a phrase http://bit.ly/aV36kb #php [...]

  2. By 10 life-saving PHP snippets on July 19, 2010 at 4:06 pm

    [...] '<strong style="background-color:yellow">$1</strong>', $sString); }Source: http://www.phpsnippets.info/highlights-words-in-a-phraseGet your average Feedburner subscribersRecently, Feedburner counts had lots of problems and [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>