Skip to content

Instantly share code, notes, and snippets.

@jwv
Created December 18, 2012 14:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwv/4328450 to your computer and use it in GitHub Desktop.
Save jwv/4328450 to your computer and use it in GitHub Desktop.
PHP: Highlight Words In A String
<?php
function highlight($astring, $aword) {
if (!is_array ($aword) || !is_string ($astring) || empty ($aword) ) {
return false;
}
$aword = implode ('|', $aword);
return preg_replace ('@\b('.$aword.')\b@si', '<strong style="background-color:red">$1</strong>', $astring);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment