I was improving a page which showed results of a freetext search. I wanted to highlight the matched text in the display with a special class. But also I wanted to preserve the case of the original .
I used a CSS class called “spotlight”, and wanted the match to be case-insensitive. So the following worked tolerably:
$title_formatted = eregi_replace($query, "<span class=\"spotlight\">$query</span>", $title);
However, the results were slightly marred by the fact that the target text (which got put back in the string between the tags), inherited the case of the query string. If someone queried for “west” then the resultant title became:
Web development in the South <span class="spotlight">west</span>
which loses the capital “W” in “West”.
eregi_replace($query, "<span class=\\\"spotlight\">\\0</span>", $title);
The regexp of “\\0” means “take the token which made the first match, and insert it here into the target string”. This preserves the capital "W" in “West”