Skip to content

Instantly share code, notes, and snippets.

@apphp-snippets
Created May 20, 2012 20:50
Show Gist options
  • Save apphp-snippets/2759487 to your computer and use it in GitHub Desktop.
Save apphp-snippets/2759487 to your computer and use it in GitHub Desktop.
You may use this code to highlight specific words in your displaying search results
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
function highlight($str, $words){
if(!is_array($words) || empty($words) || !is_string($str)){
return false;
}
$arr_words = implode('|', $words);
return preg_replace(
'@\b('.$arr_words.')\b@si',
'<strong style="background-color:yellow">$1</strong>',
$str
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment