Case insensitive in php

I use this function to highlight mysql query results:

 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);

the problem is that if I type “good”, it will only show my search results in lowercase “g'ood” and not “Good”. How to fix it?

+3
source share
1 answer

Use str_ireplace()instead.

EDIT: here is the version of regexp that stores the original case:

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

Source: https://habr.com/ru/post/1743640/


All Articles