PHP는 간단한 소스 코드 구문 강조 기능을 구현합니다.

WBOY
풀어 주다: 2016-07-25 08:45:32
원래의
948명이 탐색했습니다.

一个php实现的简单语法高亮显示的函数,注意:这个函数设计的比较简单,可能对某些语法不能高亮显示,你可以自己扩充该函数的功能

  1. function syntax_highlight($code){
  2. // this matches --> "foobar" <--
  3. $code = preg_replace(
  4. '/"(.*?)"/U',
  5. '""', $code
  6. );
  7. // hightlight functions and other structures like --> function foobar() <---
  8. $code = preg_replace(
  9. '/(s)b(.*?)((b|s)()/U',
  10. '',
  11. $code
  12. );
  13. // Match comments (like /* */):
  14. $code = preg_replace(
  15. '/(//)(. )s/',
  16. '
    ',
  17. $code
  18. );
  19. $code = preg_replace(
  20. '/(/*.*?*/)/s',
  21. '
',
  • $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";
  • print $_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 '
    ';</li>
    <li>print syntax_highlight($example_php_code);</li>
    <li>print '
    ';
  • /*example-end*/
  • 复制代码

    PHP


    원천:php.cn
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!