前端之家收集整理的这篇文章主要介绍了
php 给指定关键词添加span标签,高亮显示关键词示例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对
PHP给一组指定的关键词
添加span
标签,高亮突出
显示关键词感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
/**
* 给一组指定的关键词添加span标签,高亮突出显示关键词
* Example use: $spanned = codeWords($string_containing_keywords);
* Using colour==blue,but different arrays of words and different
* colours can be added.
* @param
* @author 网: www.512Pic.com
**/
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<span style='color:$color'>" . $value . "</span>";
}
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;
}
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
原文链接:https://www.f2er.com/php/528680.html