Php: how to add a span tag to a group of specified keywords _ PHP Tutorial

Source: Internet
Author: User
Php adds a span tag to a group of specified keywords. Php adds a span tag to a group of specified keywords as follows: Here, php adds a span tag to a group of specified keywords, highlighted keyword 12345678910111 php adds a span tag to a group of specified keywords

The details are as follows:

In this example, php adds a span tag to a group of specified keywords to highlight and highlight keywords.

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

// Example use: $ spanned = codeWords ($ string_containing_keywords );

// My site: andrew. dx. am

// Using color = blue, but different arrays of words and different

// Colours can be added.

Function onlyWholeWords (& $ value, $ key ){

// Ignores words after // comment delimiters.

// $ Value = "/\ B (". $ value. ") \ B/"; // doesn't handle comments

// $ Value = "/^ (? :(?! \/).) * \ K \ B (". $ value.") \ B /";

// \ K lookbehind alternative is not supported in PHP <5.2.4, so use:

$ Value = "/^ ((? :(?! \/).) *) \ B ". $ value." \ B /";

}

Function addSpan (& $ value, $ key, $ color = 'blue '){

$ Value = "$1". $ value ."";

}

Function codeWords ($ code ){

$ Keywords = array ('as', 'break', 'case', 'class ',

'Continue ', 'default', 'Do', 'elif', 'else ',

'Elseif', 'for', 'foreach', 'function', 'if ',

'New', 'null', 'Return ', 'self', 'Switch ',

'This', 'to', 'typeof ', 'Until ',

'Var', 'void', 'wait', 'with ');

$ Keywords2 = $ keywords;

Array_walk ($ keywords, 'onlywholeword ');

Array_walk ($ keywords2, 'addspan ', 'blue ');

$ Code = preg_replace ($ keywords, $ keywords2, $ code );

Return $ code;

}

The specific example is as follows: Here, php adds a span tag to a group of specified keywords, highlighting and highlighting keywords 1 2 3 4 5 6 7 8 9 10 11 1...

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.