3

この関数を使用して、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」でのみ表示され、「Good」では表示されないことです。これを修正するにはどうすればよいですか?

4

1 に答える 1

20

str_ireplace()代わりに使用してください。

編集:これは元のケースを保持する正規表現バージョンです:

$string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</span>", $string);
于 2010-05-01T16:46:29.693 に答える