This article illustrates the simple syntax highlighting function of PHP implementation. Share to everyone for your reference. The specific analysis is as follows:
This is a PHP implementation of the simple syntax highlighting function, Note: This function design is relatively simple, may not be highlighted in some syntax, you can expand the function
function Syntax_highlight ($code) {//This matches--> "Foobar" <--$code = preg_replace ('/"(. *?)"
/u ', ' "<span style=" color: #007F00 ">$1</span>", $code); Hightlight functions and other structures like--> function foobar () <---$code = preg_replace ('/(\s) \b (. *?)
((\b|\s) \/u ', ' $1<span style= ' color: #0000ff ">$2</span>$3", $code); Match Comments (like/* * *): $code = Preg_replace ('/(\/\/) (. +) \s/', ' <span style= ' color: #660066; background -color: #FFFCB1; "
><i>$0</i></span> ', $code); $code = Preg_replace ('/(\/\*.*?\*\/)/s ', ' <span style= ' color: #660066; Background-color: #FFFCB1; "
><i>$0</i></span> ', $code);
Hightlight braces: $code = Preg_replace ('/(|\[|\{|\}|\]|\) |\->)/', ' <strong>$1</strong> ', $code); Hightlight variables $foobar $code = preg_replace ('/(\$[a-za-z0-9_]+)/', ' <span style= ' color: #0000B3 ">$1< /span> ', $code); /* The \b in the pattern indicates a word boundary, so only the distinct * * * word "web" is matched, and not a word partial Like "webbing" or "cobweb"///special words and functions $code = Preg_replace ('/\b (print|echo|new|function) \b/
', ' <span style= ' color: #7F007F ' >$1</span> ', $code);
return $code; /*example-start*//* * * Create Some example PHP code: */$example _php_code = '//Some code comment: $example = "Foobar"
;
Print $_server["REMOTE_ADDR"];
$array = Array (1, 2, 3, 4, 5);
function Example_function ($STR) {//Reverse string echo strrev ($obj);} Print example_function ("foo"); * * * * * * Multiple line comment/print "something:".
$example; ';
Output the formatted code:print ' <pre> ';
Print syntax_highlight ($example _php_code);
print ' </pre> '; /*example-end*/
I hope this article will help you with your PHP programming.