views:

31

answers:

1

Possible Duplicate:
highlighting search results in php/mysql

I am doing a search with a query, in MSSQL, like this:

SELECT ctext FROM Table WHERE ctext like '%filter%'

Then, I am wanting to highlight the hits with php:

 function highlightme($str, $filter){
     $html = "<FONT style=".chr(34)."BACKGROUND-COLOR: yellow".
     chr(34).">".$filter."</FONT></P>";
     $buf = str_replace($filter,$html,$str);
     return $buf;
 }

But, if the filter was 'Hello', and ctext contains 'hello', the SQL brings it, but php does not highlight it (I think it has to do with case sensitive) And, If the filter was, 'hellos' and ctext contains 'hello', the SQL brings it, but php does not hightlight it either.

How can I solve this two things??

+1  A: 

First of all, instead of using chr(34) you should use "\"" or '"'. I suggest that instead of using str_replace you use preg_replace, which allows you to specify a regexp pattern. You can use the i modifier to make the pattern case-insensitive.

Example:

CSS

.keyword {
    color: yellow;
}

PHP

$pattern = '#(hello)#i';
$replacement = '<span class="keyword">\1</span>';
$result = preg_replace($pattern, $replacement, $input);

It is also possible to specify pattern and replacement as arrays.

igorw