本文实例讲述了php给一组指定关键词添加span标签的方法。分享给大家供大家参考。具体如下:

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

// 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 = "/^(" . $value . ")\b/"; 
// \K lookbehind alternative is not supported in PHP < 5.2.4, so use:
  $value = "/^((" . $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;
}

希望本文所述对大家的php程序设计有所帮助。

标签:
php,指定关键词,添加,span标签

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com