Created
December 18, 2012 14:29
-
-
Save jwv/4328450 to your computer and use it in GitHub Desktop.
PHP: Highlight Words In A String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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