How PHP adds a span tag to a specified set of keywords _php tutorial

Source: Internet
Author: User

How PHP adds a span tag to a specified set of keywords


Specific as follows:

This is PHP to a set of keywords to add a span tag, highlighting keywords

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

Example use: $spanned = codewords ($string _containing_keywords);

My site:andrew.dx.am

Using Colour==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 = "$". $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 ', ' while ', ' with ');

$keywords 2 = $keywords;

Array_walk ($keywords, ' onlywholewords ');

Array_walk ($keywords 2, ' Addspan ', ' Blue ');

$code = Preg_replace ($keywords, $keywords 2, $code);

return $code;

}

http://www.bkjia.com/PHPjc/977165.html www.bkjia.com true http://www.bkjia.com/PHPjc/977165.html techarticle the way PHP adds a span tag to a specified set of keywords is as follows: PHP adds a span tag to a specified set of keywords, highlighting keywords 1 2 3 4 5 6 7 8 9 all 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.