PHP to implement simple syntax highlighting function example analysis _php skills

Source: Internet
Author: User
Tags php programming

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.

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.