I have a number of HTML fragments in a database, similar to this:
$html = "<P align=center>I went to the shopping center yesterday.</P>";
I have created a search page which allows users to do keyword searches, and I would like to apply bold formatting to matching keywords within the HTML fragments. Here's an example illustrating what I have so far:
$keyword = "shopping";
$html = eregi_replace("($keyword)", "<B>\1</B>", $html);
This works fine. It will put bold tags around the word 'shopping'.
The problem is that this regex also matches keywords that appear inside of HTML tags.
If you change $keyword to "center" in the above example, it will put bold tags inside of the <P align=center> tag. It screws everything up.
Is there some way I can restrict my regex so that it will not bold keyword matches that appear within HTML tags?
I have been struggling with this for hours, and I normally don't like to ask for help... but I really could use some ideas or direction in this case. Any suggestions would be vastly appreciated.