Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: VB.Net/C# BBCode Function

Results 1 to 4 of 4
Share This Thread →
  1. #1
    jmurrayhead is offline Moderator
    Join Date
    Feb 2004
    Location
    Reston, VA
    Posts
    13,248
    Rep Power
    1907

    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:
     
        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:
     
    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
    Attached Files Attached Files
    Last edited by jmurrayhead; November 15th, 2007 at 09:53 AM. Reason: added samples
    jmurrayhead

  2. #2
    Memnoch's Avatar
    Memnoch is offline Unholy Moderator
    Join Date
    Oct 2003
    Location
    In hell, where did you think?
    Posts
    12,026
    Rep Power
    658
    Do you have a functioning prototype that can be tested?

  3. #3
    jmurrayhead is offline Moderator
    Join Date
    Feb 2004
    Location
    Reston, VA
    Posts
    13,248
    Rep Power
    1907
    Quote Originally Posted by Memnoch
    Do you have a functioning prototype that can be tested?
    I just attached a quick sample to my original post. Very basic, but does the job

  4. #4
    Dax Bane is offline Registered User
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Thumbs up

    Nice code

    I notice that the code doesn't handle nested tags in the most ideal manner (for example, nesting an "I" tag inside a "U", but closing the "U" before closing the "I") - perhaps using a Generic.Stack to maintain tag nesting would solve this?

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. Java Script Header problem
    By chen in forum HTML, JavaScript And CSS Help
    Replies: 8
    Last Post: November 2nd, 2006, 06:26 AM
  2. Calling functions
    By 3cia in forum ASP Development
    Replies: 3
    Last Post: August 21st, 2006, 05:57 AM
  3. Object for printing problems with WebForms
    By tudela in forum Code Bank
    Replies: 2
    Last Post: July 28th, 2005, 09:20 AM
  4. Date function on database fields
    By Lexy in forum Visual Basic Programming
    Replies: 1
    Last Post: March 22nd, 2005, 11:01 AM
  5. Adding to a Session variable (or Array!)
    By rehanbutt in forum ASP Development
    Replies: 1
    Last Post: June 7th, 2004, 09:15 AM

ASP Free Advertisers and Affiliates