php将文中关键词高亮显示,可以用正则表达式

    $text = "Sample sentence from AnsonCheung.tk, regular expression has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";

    $text = preg_replace("/\b(regex)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);

    echo $text;

结果:

php中将文中关键词高亮显示,快捷方式可以用正则-LMLPHP

05-11 11:22