DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Programming & Scripting > Code Samples > .Net

Discuss "VB.Net/C# BBCode Function" in the .Net forum.

.Net - Post your ASP.net and Windows Forms code samples here.


Reply « Previous Thread | Next Thread »  
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old March 20th, 2008, 09:44 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 530
Thanks: 14
Thanked 38 Times in 37 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead is on a distinguished road

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default 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:
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="http://www.developerbarn.com/asp-net/"$1"">$1</a>")
 
        '//Regex for URL with anchor
        regExp = New Regex("\[url=([^\]]+)\]([^\]]+)\[\/url\]")
        strTextToReplace = regExp.Replace(strTextToReplace, "<a href="http://www.developerbarn.com/asp-net/"$1"">$2</a>")
 
        '//Image regex
        regExp = New Regex("\[img\]([^\]]+)\[\/img\]")
        strTextToReplace = regExp.Replace(strTextToReplace, "<img src="http://www.developerbarn.com/asp-net/"$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:
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://www.developerbarn.com]DeveloperBarn 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.
__________________
jmurrayhead
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Reply With Quote
Sponsored Links
Reply

  DeveloperBarn Forums > Programming & Scripting > Code Samples > .Net

Bookmarks

Tags
bbcode, function, vb.net

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/net/40-visual-basic-net-c-bbcode-function.html
Posted By For Type Date
ASP.NET Zone - bbCode mit RegEx ersetzen This thread Refback July 16th, 2008 10:01 PM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 07:39 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46