|
// Example use: $ spanned = codeWords ($ string_containing_keywords );
// My site: andrew. dx. am
// Using color = 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 = "$1". $ 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', 'wait', 'with ');
$ Keywords2 = $ keywords;
Array_walk ($ keywords, 'onlywholeword ');
Array_walk ($ keywords2, 'addspan ', 'blue ');
$ Code = preg_replace ($ keywords, $ keywords2, $ code );
Return $ code;
}
|