The example in this article describes how PHP adds a span label to a set of specified keywords. Share to everyone for your reference. Specifically as follows:
Here is PHP to add a span label to a set of keywords, highlighting the keyword
Example use: $spanned = codewords ($string _containing_keywords);
My site:andrew.dx.am//Using colour==blue, but different arrays to words and different//colours can be added. Function Onlywholewords (& $value, $key) {//ignores words after//comment delimiters.//$value = "/\b (". $value. ") \b/"; Doesn ' t handle comments//$value = "/^ (?:(?! \/\/).) *\k\b (". $value.
") \b/"; \k Lookbehind Alternative isn't supported in PHP < 5.2.4, so use: $value = "/^ (?:(?! \/\/).) *) \b ". $value.
"\b/"; Function Addspan (& $value, $key, $color = ' blue ') {$value = ' $1<span style= ' color: $color ' > '. $value.
"</span>"; function codewords ($code) {$keywords = array (' As ', ' break ', ' case ', ' class ', ' Continue ', ' default ', ' Do ', ' elif ', ' Else ', ' ElseIf ', ' for ', ' foreach ', ' function ', ' if ', ' new ', ' null ', ' return ', ' self ', ' switch ', ' that ', ' to ', ' type
of ', ' until ', ' var ', ' void ', ' while ', ' with ';
$keywords 2 = $keywords; Array_walk ($keywords, ' onLywholewords ');
Array_walk ($keywords 2, ' Addspan ', ' Blue ');
$code = Preg_replace ($keywords, $keywords 2, $code);
return $code; }
I hope this article will help you with your PHP programming.