0

I'm working on a search function and I'm stuck on something. I currently have the following query executed in php and displayed with simple echo:

SELECT * FROM `artist` WHERE `ArtistName` LIKE '% ".$searchfield."%'

It only selects matches that are the same in the beginning of words. For example, if someone types "a", it will return just names like "Adam" or "Jason Aldean".

What I now want to accomplish is that the searchterm is highlighted with a span or simple b tag.

I have tried doing it like this:

$display= str_ireplace($searchfield,"<b'>".$searchfield."</b>",$full);

But in this way, in my example, it will replace all the the a's in the result. What you will get then is this: J<b>a</b>son <b>A</b>lde<b>a</b>n while I just want this Jason <b>A</b>ldean. Because that's also how the query selected the row.

I hope this makes sense, I'll be happy to try any suggestions.

Thank you all in advance. - Yordi

  • Are you sure about that? Note that there is a space between the first % and ". The results from the query looked to be correct though. – Yordi Verbeeck Apr 05 '15 at 21:20
  • My apologies, you are correct. I did not initially observe the space! – Ohgodwhy Apr 05 '15 at 21:23
  • Look at http://stackoverflow.com/questions/1252693/using-str-replace-so-that-it-only-acts-on-the-first-match for a solution. – Norbert Apr 06 '15 at 01:53

0 Answers0