views:

8

answers:

1

I am building a search that will wrap the searched text with a <span> tag and i have this code working ok:

str_ireplace($q,'<span>'.$q.'</span>',$row[name]);

The problem is, if a user searches for Tom is will show Tom which is cool, but if they put in tom because of the str_ireplace it would show tom, does that make sense? The real issue is if someone entered tOm aRnFeLd although it would search ok, the aesthetics would actually show up on the page tOm aRnFeLd

How can i retain the capital letters and lower case 'ness of both strings? Is there a better way to wrap case insensitive text from a string?

A: 
  1. Get phrase length strlen()
  2. Find occurrence of phrase using stripos()
  3. Insert into text <span> after text's N character (where N is result from point #2)
  4. Insert into text </span> after text's N+M character (where N is result from point #2 and M is result from point #1)
  5. Continue points 2-4 (Using third parameter of strpos() - offset)
Crozin