« Back to profile of aimad

  • php

    Highlight specific words in a phrase

    Sometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do:

    01:22 Jul 20 2010 | Tags :
    						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);
}    			         
Owned Code : Phpsnippets
Add comment
To add a comment, please : Login or Sign up
    aimad
    Aimadj
  • New visitor?

    On setCode, you can save your codes, functions and classes, you can also share them with your friends, and track your favorite developer

    Join the SetCode community
  • Your language here :