php中不区分大小写的突出显示

php中不区分大小写的突出显示,php,search,highlight,case-insensitive,Php,Search,Highlight,Case Insensitive,我使用此函数突出显示mysql查询的结果: function highlightWords($string, $word) { $string = str_replace($word, "<span class='highlight'>".$word."</span>", $string); /*** return the highlighted string ***/ return $string; } .... $c

我使用此函数突出显示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);
函数highlightWords($string,$word)
{
$string=str_replace($word,“$word.”,$string);
/***返回突出显示的字符串***/
返回$string;
}
....
$cQuote=highlightWords(htmlspecialchars($row['cQuotes']),$search\u result);
问题是,如果我输入'good',它只会以小写字母'good'和'good'显示我的搜索结果。如何纠正此问题?

请改用
str\u ireplace()

编辑:以下是保留原始大小写的regexp版本:

$string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</span>", $string);
$string=preg\u replace(“/”.preg\u quote($word,“/”)。/i“,$0“,$string);

这是可行的,但它的作用是将大写字母“G”改为小写字母“G”。还有其他想法吗?你应该使用
preg_quote($word,“/”)
,而不仅仅是
$word
@FrancisMV123来匹配第一个参数