PHP implementation of simple syntax highlighting function example analysis, highlight example Analysis _php tutorial

Source: Internet
Author: User

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: ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.