PHP中不区分大小写的突出显示 [英] case insensitive highlighting in php

查看:100
本文介绍了PHP中不区分大小写的突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此函数突出显示mysql查询的结果:

i'm using this function to highlight the results from mysql query:

 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而不是好。我该如何纠正?

the problem is, if i type in 'good', it will only show my search results with a lower-case 'g'ood and not 'Good'. how do i rectify this?

推荐答案

使用 str_ireplace()代替。

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

Here is regexp version that keeps original case:

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

这篇关于PHP中不区分大小写的突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆