Home > Backend Development > PHP Tutorial > How to add span tags to a set of specified keywords in php_PHP tutorial

How to add span tags to a set of specified keywords in php_PHP tutorial

WBOY
Release: 2016-07-13 09:58:45
Original
829 people have browsed it

How to add span tags to a set of specified keywords in php

The details are as follows:

Here is php adding span tags to a set of specified keywords and highlighting the 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 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 = "/^(?:(?!//).)*Kb(" . $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');

$keywords2 = $keywords;

array_walk($keywords, 'onlyWholeWords');

array_walk($keywords2, 'addSpan', 'blue');

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

return $code;

}

1 2 3

4

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 // colors can be added. function onlyWholeWords(&$value, $key) { // Ignores words after // comment delimiters. //$value = "/b(" . $value . ")b/"; // doesn't handle comments //$value = "/^(?:(?!//).)*Kb(" . $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', 'while', 'with'); $keywords2 = $keywords; array_walk($keywords, 'onlyWholeWords'); array_walk($keywords2, 'addSpan', 'blue'); $code = preg_replace($keywords, $keywords2, $code); return $code; }
http://www.bkjia.com/PHPjc/977165.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/977165.htmlTechArticleThe method for php to add span tags to a group of specified keywords is as follows: Here is php to a group of specified keys Add span tags to words and highlight keywords 1 2 3 4 5 6 7 8 9 10 11 1...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template