I have a simple, site-specific search engine.

It finds matches in a MySQL database using LIKE.

Then it bolds the search terms before displaying the results to the user. And it also renders URLs clickable.

So, say the user has searched for "perlmonks" and the page contains "you should all go to http://perlmonks.org, it's great!".

We find the term in the database, we bold it, we render the URL clickable and display it to the user, but at this point, the HTML has become this:

you should all go to <a href="http://<b>perlmonks</b>.org">http://<b>perlmonks</b>.org</a>, it's great!

Which is of course an invalid URL. How would the Monks approach this problem? ...

Update: I've got some useful and some ... non-useful replies to this. Anonymous monk doesn't seem to understand the question at all. JavaFan doesn't either, although that's my fault because my code was wrong. I updated it.

ikegami and mr_mischief, thank you, but I guess it comes down to this: I can't do this with a simple regular expression, can I? I need some kind of parsing where the regular expression would only be applied to things not inside HTML brackets. Or can I do it with an evaluated RHS?

In reply to Bolding search terms ... which might be URLs? by Cody Fendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.