Hi guys
I’m currently working on a function that will convert BBCode into HTML tags.
It almost works perfectly but for some reason it’s unable to handle nested tags (ie tags within another set of tags).
For example, I submit the following BBCode:
[center][b][u][color=#ff3333][i]Here is my description[/i][/color][/u][/b][/center]
but the output on my page is:
<center><span style="font-weight: bold;"><span style="text-decoration: underline;">[color=#ff3333]<span style=" font-style: italic;">Here is my description</span>[/color]</span></span></center>
It just can’t handle the [color=#ff3333] tags being nested within other tags.
My code can be seen below
Is there any way of rectifying this issue? I have been on this for about 5 days now and just can’t figure it out. Any help would be much appreciated
Best regards
Rod from the UK
<%
'----------------------------------------
' Common Regular Expression Function
'----------------------------------------
Function ReplaceRegExp(strString, strPattern, strReplace)
Dim RE: Set RE = New RegExp
With RE
.Pattern = strPattern
.Global = True
ReplaceRegExp = .Replace(strString, strReplace)
End With
End Function
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'----------------------------------------
' Turn BBcode into HTML
'----------------------------------------
Function BBCodeToHTML(strString)
strString = ReplaceRegExp(strString, "(http|ftp|https)(:\/\/[\w\-_]+)((\.[\w\-_]+)+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?", "<a href=""$1$2$3$5"" target=""_blank"">$1$2$3$5</a>")
strString = ReplaceRegExp(strString, "\[color=([^\]]*)\]([^\[]*)\[/color\]", "<span style=""color: $1;"">$2</span>")
strString = ReplaceRegExp(strString, "\[size=([^\]]*)\]([^\[]*)\[/size\]", "<font size=""$1"">$2</font>")
strString = ReplaceRegExp(strString, "\[font=([^\]]*)\]([^\[]*)\[/font\]", "<span style=""font-family: $1, Sans-Serif, Serif;"">$2</span>")
strString = ReplaceRegExp(strString, "\[quote=([^\]]*)\]([^\[]*)\[/quote\]", "<div class=""quote""><span style=""font-weight: bold; font-size: 8pt;"">$1 said:</span><pre class=""quote1"">$2</pre></div>")
strString = Replace(strString, "[b]", "<span style=""font-weight: bold;"">")
strString = Replace(strString, "[B]", "<span style=""font-weight: bold;"">")
strString = Replace(strString, "[u]", "<span style=""text-decoration: underline;"">")
strString = Replace(strString, "[U]", "<span style=""text-decoration: underline;"">")
strString = Replace(strString, "[i]", "<span style="" font-style: italic;"">")
strString = Replace(strString, "[I]", "<span style="" font-style: italic;"">")
strString = Replace(strString, "[/b]", "</span>")
strString = Replace(strString, "[/B]", "</span>")
strString = Replace(strString, "[/u]", "</span>")
strString = Replace(strString, "[/U]", "</span>")
strString = Replace(strString, "[/i]", "</span>")
strString = Replace(strString, "[/I]", "</span>")
strString = Replace(strString, "[li]", "<li>")
strString = Replace(strString, "[/li]", "</li>")
strString = Replace(strString, "[ol]", "<ol>")
strString = Replace(strString, "[/ol]", "</ol>")
strString = Replace(strString, "[ul]", "<ul>")
strString = Replace(strString, "[/ul]", "</ul>")
strString = Replace(strString, "[center]", "<center>")
strString = Replace(strString, "[/center]", "</center>")
BBCodeToHTML = strString
End Function
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
strTest = "[center][b][u][color=#ff3333][i]Here is my description[/i][/color][/u][/b][/center]"
description = BBCodeToHTML(strTest)
%>
<%=description%>