【问题标题】:case insensitive highlighting in phpphp中不区分大小写的突出显示
【发布时间】:2011-02-14 14:19:21
【问题描述】:

我正在使用这个函数来突出 mysql 查询的结果:

 function highlightWords($string, $word)
 {

        $string = str_replace($word, "<span class='highlight'>".$word."</span>", $string);
    /*** return the highlighted string ***/
    return $string;

 }

 ....

  $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);

问题是,如果我输入“good”,它只会以小写的“g”ood 而不是“Good”显示我的搜索结果。我该如何纠正这个问题?

【问题讨论】:

    标签: php search highlight case-insensitive


    【解决方案1】:

    请改用str_ireplace()

    编辑:这是保留原始大小写的正则表达式版本:

    $string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</span>", $string);
    

    【讨论】:

    • 这行得通,但它的作用是将大写的“G”更改为小写的“g”。还有其他想法吗?
    • 您应该使用preg_quote($word, '/') 而不仅仅是$word
    • @FrancisMV123 第一个参数的匹配
    • 非常感谢!在使用正则表达式头痛一小时后得到帮助
    猜你喜欢
    • 2018-02-28
    • 2023-03-06
    • 2015-06-11
    • 2013-08-16
    • 2013-10-04
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多