Php implements simple syntax highlighting function instance analysis

Source: Internet
Author: User

Php implements simple syntax highlighting function instance analysis

This article mainly introduces php to implement simple syntax highlighting functions. The example analyzes php's skills related to syntax highlighting through regular expressions. For more information, see

This example describes how php implements simple syntax highlighting functions. Share it with you for your reference. The specific analysis is as follows:

This is 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

'"<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: #20.b3"> $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 ");

/*

** A 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 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.