Php implements simple syntax highlighting functions

Source: Internet
Author: User
Php implements simple syntax highlighting functions

A simple syntax highlighted function implemented by php. note: This function is easy to design and may not be highlighted for some syntaxes. you can expand the function by yourself.

  1. Function syntax_highlight ($ code ){
  2. // This matches --> "foobar" <--
  3. $ Code = preg_replace (
  4. '/"(.*?) "/U ',
  5. '"$1"', $ code
  6. );
  7. // Hightlight functions and other structures like --> function foobar () <---
  8. $ Code = preg_replace (
  9. '/(\ S) \ B (.*?) (\ B | \ s) \ ()/U ',
  10. '$1 $2 $3 ',
  11. $ Code
  12. );
  13. // Match comments (like /**/):
  14. $ Code = preg_replace (
  15. '/(\/) (. +) \ S /',
  16. '$0',
  17. $ Code
  18. );
  19. $ Code = preg_replace (
  20. '/(\/\*.*? \ * \/)/S ',
  21. '$0',
  22. $ Code
  23. );
  24. // Hightlight braces:
  25. $ Code = preg_replace ('/(\ (| \ [|\{|\}|\] |\) |\-> )/','$1', $ Code );
  26. // Hightlight variables $ foobar
  27. $ Code = preg_replace (
  28. '/(\ $[A-zA-Z0-9 _] +)/', '$ 1', $ code
  29. );
  30. /* The \ B in the pattern indicates a word boundary, so only the distinct
  31. ** Word "web" is matched, and not a word partial like "webbing" or "cobweb"
  32. */
  33. // Special words and functions
  34. $ Code = preg_replace (
  35. '/\ B (print | echo | new | function) \ B /',
  36. '$ 1', $ code
  37. );
  38. Return $ code;
  39. }
  40. /* Example-start */
  41. /*
  42. ** Create some example PHP code:
  43. */
  44. $ Example_php_code ='
  45. // Some code comment:
  46. $ Example = "foobar ";
  47. Print $ _ SERVER ["REMOTE_ADDR"];
  48. $ Array = array (1, 2, 3, 4, 5 );
  49. Function example_function ($ str ){
  50. // Reverse string
  51. Echo strrev ($ obj );
  52. }
  53. Print example_function ("foo ");
  54. /*
  55. ** A multiple line comment
  56. */
  57. Print "Something:". $ example ;';
  58. // Output the formatted code:
  59. Print'
    ';
  60. print syntax_highlight($example_php_code);
  61. print '
  62. ';
  63. /* Example-end */


Php

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.