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

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;  

 结果:

posted on 2014-04-18 09:18  菜鸟丫丫  阅读(325)  评论(0编辑  收藏  举报

导航