网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 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 = "/^(?:(?!\/\/).)*\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;
    }
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/16 1:44:20