| Example use: $spanned = codewords ($string _containing_keywords); My site:andrew.dx.am Using Colour==blue, but different arrays of 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 is not supported in PHP < 5.2.4, so use: $value = "/^ (?:(?! \/\/).) *) \b ". $value. "\b/"; } Function Addspan (& $value, $key, $color = ' blue ') { $value = "$". $value. ""; } function codewords ($code) { $keywords = Array (' As ', ' break ', ' case ', ' class ', ' Continue ', ' default ', ' Do ', ' elif ', ' Else ', ' ElseIf ', ' for ', ' foreach ', ' function ', ' if ', ' new ', ' null ', ' return ', ' self ', ' switch ', ' This ', ' to ', ' typeof ', ' 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; } |