首页 >后端开发 >php教程 >php给一组指定关键词添加span标签的方法_PHP教程

php给一组指定关键词添加span标签的方法_PHP教程

WBOY
WBOY原创
2016-07-13 09:58:45831浏览

php给一组指定关键词添加span标签的方法

 具体如下:

这里是php给一组指定的关键词添加span标签,高亮突出显示关键词

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 = "/^(?:(?!\/\/).)*\K\b(" . $value . ")\b/";

// \K lookbehind alternative is not supported in PHP

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

}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/977165.htmlTechArticlephp给一组指定关键词添加span标签的方法 具体如下: 这里是php给一组指定的关键词添加span标签,高亮突出显示关键词 1 2 3 4 5 6 7 8 9 10 11 1...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn