BBCode to XHTML: Two possibilities, one regex?
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://stackov开发者_高级运维erflow.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:
\[url\s*=\s*([^\]]*)]\s*((?:(?!\s*\[/url\]).)*)\s*\[/url\]|\[url\s*=\s*([^\]]*)]
Replacement pattern:
<a href="$1$3" title="$2">$2$3</a>
Tested with this input:
bla [url=http://stackoverflow.com/]StackOverflow[/url] bla
bla [url=http://stackoverflow.com/] bla
Returns:
bla <a href="http://stackoverflow.com/" title="StackOverflow">StackOverflow</a> bla
bla <a href="http://stackoverflow.com/" title="">http://stackoverflow.com/</a> bla
Note that in any case you may have to add some validation/escaping, as invalid XML characters (", <, > etc.) may "break" the tag contents.
Something like this ghastly piece of work should do it:
\[url=([^\]]+)\](?:([^\[]+)\[\/url\])?
Upon matching, this should place the url in $1 and the text in $2 if it has been specified. I didn't test this yet so it could require some tweaking.
Analize function BBCode2HTML($text)
https://code.google.com/p/pwision/source/browse/trunk/inc/BBCode.inc#150
The algorithm to transform BBCode 2 HTML is: 1) regulate the BBCode text code here: https://code.google.com/p/pwision/source/browse/trunk/inc/BBCode.inc#60
2) transform the regulated BBCode to XML code here: https://code.google.com/p/pwision/source/browse/trunk/inc/BBCode.inc#101
3)run a XSLT transformation to the XML text. The XSLT file is here https://code.google.com/p/pwision/source/browse/trunk/inc/BBCodeXML2HTML.xslt The transformation is done here: https://code.google.com/p/pwision/source/browse/trunk/inc/BBCode.inc#113
Hope I helped!
PS: a good XSLT tutorial you can find here: http://www.w3schools.com/xsl/xsl_languages.asp
精彩评论