
October 19th, 2007, 12:44 PM
|
|
Moderator
|
|
Join Date: Feb 2004
Location: Reston, VA, USA
|
|
|
VB.Net/C# BBCode Function
I've looked all over the net for something like this for ASP.Net and couldn't find what I needed. After reviewing several pages, I was able to use individual pieces of information to build this simple regular expression to parse BBCode into HTML.
I thought something like this would be really long and complicated, but it's not at all:
VB.Net Code:
Original
- VB.Net Code |
|
|
|
Public Function BBCode(ByVal strTextToReplace As String) As String '//Define regex Dim regExp As Regex '//Regex for URL tag without anchor regExp = New Regex("\[url\]([^\]]+)\[\/url\]") strTextToReplace = regExp.Replace(strTextToReplace, "<a href=""$1"">$1</a>") '//Regex for URL with anchor regExp = New Regex("\[url=([^\]]+)\]([^\]]+)\[\/url\]") strTextToReplace = regExp.Replace(strTextToReplace, "<a href=""$1"">$2</a>") '//Image regex regExp = New Regex("\[img\]([^\]]+)\[\/img\]") strTextToReplace = regExp.Replace(strTextToReplace, "<img src=""$1"" />") '//Bold text regExp = New Regex("\[b\](.+?)\[\/b\]") strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>") '//Italic text regExp = New Regex("\[i\](.+?)\[\/i\]") strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>") '//Underline text regExp = New Regex("\[u\](.+?)\[\/u\]") strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>") '//Font size regExp = New Regex("\[size=([^\]]+)\]([^\]]+)\[\/size\]") strTextToReplace = regExp.Replace(strTextToReplace, "<span style=""font-size: $1px"">$2</span>") '//Font color regExp = New Regex("\[color=([^\]]+)\]([^\]]+)\[\/color\]") strTextToReplace = regExp.Replace(strTextToReplace, "<span style=""color: $1"">$2</span>") Return strTextToReplace End Function
C# Code:
Original
- C# Code |
|
|
|
public string BBCode(string strTextToReplace) { ////Define regex Regex regExp; ////Regex for URL tag without anchor regExp = new Regex(@"\[url\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>"); ////Regex for URL with anchor regExp = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>"); ////Image regex regExp = new Regex(@"\[img\]([^\]]+)\[\/img\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />"); ////Bold text regExp = new Regex(@"\[b\](.+?)\[\/b\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>"); ////Italic text regExp = new Regex(@"\[i\](.+?)\[\/i\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>"); ////Underline text regExp = new Regex(@"\[u\](.+?)\[\/u\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>"); ////Font size regExp = new Regex(@"\[size=([^\]]+)\]([^\]]+)\[\/size\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>"); ////Font color regExp = new Regex(@"\[color=([^\]]+)\]([^\]]+)\[\/color\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>"); return strTextToReplace; }
This uses block brackets to define the BBCode. For example, if you wanted to place a URL on your page, you would write it like this:
[ url=http://forums.aspfree.com]ASP Free Forums[/ url] (without the spaces in the brackets).
You can easily adapt it with other BBCode if you understand Regex. I'll add more examples as I get time.
Note: C# isn't my native language  So, if you find an error in the above C# code, let me know and I'll fix it.
Quote: | Originally Posted by UPDATES
I found a bug in the function already . When using nested BBCode, it won't always work. For example, entering:
[b ][i ]Testing[/i ][/b ]
Will place the item in italics, but will actually write the [b ][/ b] tags.
However, when doing this:
[i ][b ]Testing[/b ][/i ]
The text will be bold and italic. I will look into this when I get some time.
[UPDATE]
Okay, everyone. I fixed the above problem. I had to change up the regular expression for the bold, italic and underline BBCode. Everything appears to be working now. I've updated the files in the attachment, as well.
[UPDATE]
I've added more BBCodes to the above functions and have updated the attachment with these new BBCodes as well.
[UPDATE]
elijathegold showed me a way to make the regular expression easier to read for C#. Files are now updated to represent this
|
Last edited by jmurrayhead : November 15th, 2007 at 09:53 AM.
Reason: added samples
|