PHP implementation of simple syntax highlighting function Example Analysis _php tutorial

Source: Internet
Author: User

PHP implementation of simple syntax highlighting function example analysis


This article mainly introduces the PHP implementation of a simple syntax highlighting function, an example of PHP through regular expression to implement syntax highlighting of the relevant skills, the need for friends can refer to the next

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

function Syntax_highlight ($code) {

This matches--"Foobar" <--

$code = Preg_replace (

'/"(.*?)" /U ',

' $', $code

);

Hightlight functions and other structures--and function foobar () <---

$code = Preg_replace (

'/(\s) \b (. *?) (\b|\s) \ ()/U ',

"$$",

$code

);

Match Comments (like/* */):

$code = Preg_replace (

'/(\/\/) (. +) \s/',

'$ A ',

$code

);

$code = Preg_replace (

'/(\/\*.*?\*\/)/s ',

'$ A ',

$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 '

';

Print syntax_highlight ($example _php_code);

print '

';

/*example-end*/

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/990983.html www.bkjia.com true http://www.bkjia.com/PHPjc/990983.html techarticle PHP Implementation of simple syntax highlighting function Example analysis this article mainly introduces the PHP implementation of simple syntax highlighting function, the example of PHP through the regular expression of the syntax highlighting the relevant technology ...

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