PHP implementation of simple syntax highlighting function example analysis, highlighting case analysis
In this paper, a simple syntax highlighting function for PHP implementation is described. Share to everyone for your reference. The specific analysis is as follows:
This is a PHP implementation of a simple syntax highlighting function, Note: This function design is relatively simple, may not highlight some of the syntax, you can expand the function
function Syntax_highlight ($code) {//This matches-"foobar" <--$code = preg_replace ('/"(. *?)" /u ', ' " $"', $code); Hightlight functions and other structures like--and function foobar () <---$code = preg_replace ('/(\s) \b (. *?) ((\b|\s) \ ()/U ', ' $ $$ ', $code); Match Comments (like/*/): $code = Preg_replace ('/(\/\/) (. +) \s/', ' $', $code); $code = Preg_replace ('/(\/\*.*?\*\/)/s ', ' $', $code); Hightlight braces: $code = Preg_replace ('/(\ (|\[|\{|\}|\]|\) |\->)/', ' $', $code); Hightlight variables $foobar $code = preg_replace ('/(\$[a-za-z0-9_]+)/', ' $', $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/', ' $', $code); return $code;} /*example-start*//*** Create Some example PHP code:*/$example _php_code = '//Some code comment: $example = "Foobar";p rint $ _server["REMOTE_ADDR"]; $array = Array (1, 2, 3, 4, 5), function example_function ($STR) {//Reverse string echo strrev ($obj) ;} Print example_function ("foo");/*** A multiple Line Comment*/print "Something:". $example; '; /Output the formatted Code:print '';p rint syntax_highlight ($example _php_code);p rint '
';/*example-end*/
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/990538.html www.bkjia.com true http://www.bkjia.com/PHPjc/990538.html techarticle PHP to implement a simple example of syntax highlighting function analysis, highlighting examples of this paper describes the PHP implementation of simple syntax highlighting function. Share to everyone for your reference. The specific analysis is as follows: ...