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??
First of all, instead of using
chr(34)you should use"\""or'"'. I suggest that instead of usingstr_replaceyou usepreg_replace, which allows you to specify a regexp pattern. You can use theimodifier to make the pattern case-insensitive.Example:
CSS
PHP
It is also possible to specify pattern and replacement as arrays.