views:

89

answers:

1

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

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?

+7  A: 

Use str_ireplace() instead.

EDIT: Here is regexp version that keeps original case:

$string = preg_replace("/".preg_quote($word, "/")."/i", "<span class='highlight'>$0</span>", $string);
serg
that works, but what it does is that it changes the capital 'G' to small 'g'. any other ideas?
fuz3d
You need regexp for that then. See edited answer.
serg
You should use `preg_quote($word, '/')` instead of just `$word`.
MiffTheFox
MiffTheFox is right, added to the answer, thanks.
serg
excellent! thank you!
fuz3d