Perfect Highlighting Function

Submitted by:Jhon Smith

Date added:05 August, 2014

Category:PHP

PHP code example to highlight words in a string.

Tags: highlight words , highlighting function

Code Snippet:

<?PHP

// highlight words in a string
FUNCTION highlight($text, $search) {
$text = PREG_REPLACE( "/(^|\s|,!|;)(".PREG_QUOTE($search, "/").")(\s|,|!|&|$)/i", "\\1<span class='hlstyle'>\\2</span>\\3", $text );
RETURN $text;
}

?>
 
 

Comments