I’m writing a small blog module. I want the users to be able to type BBCode. I need to convert that to XHTML to store in the DB, which I managed to do for most of the tags, except for [url].
There are two cases I want to allow:
[url=http://stackoverflow.com/]
which should be converted to
<a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a>
and
[url=http://stackoverflow.com/]StackOverflow[/url]
which should be converted to
<a href="http://www.stackoverflow.com" title="StackOverflow">StackOverflow</a>
Sadly, I have been unable to do that. The results where horrible, and I’m wondering if this could be done in one regex or if it has to be split in two.
This should work:
Replacement pattern:
Tested with this input:
Returns:
Note that in any case you may have to add some validation/escaping, as invalid XML characters (“, <, > etc.) may “break” the tag contents.