Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 19th, 2007, 12:44 PM
jmurrayhead jmurrayhead is offline
Moderator
Click here for more information.
 
Join Date: Feb 2004
Location: Reston, VA, USA
Posts: 13,090 jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)  Folding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced Folder
Time spent in forums: 3 Months 1 Week 6 h 32 m 27 sec
Reputation Power: 1580
Facebook
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
  1.  
  2.     Public Function BBCode(ByVal strTextToReplace As String) As String
  3.  
  4.         '//Define regex
  5.         Dim regExp As Regex
  6.  
  7.         '//Regex for URL tag without anchor
  8.         regExp = New Regex("\[url\]([^\]]+)\[\/url\]")
  9.         strTextToReplace = regExp.Replace(strTextToReplace, "<a href=""$1"">$1</a>")
  10.  
  11.         '//Regex for URL with anchor
  12.         regExp = New Regex("\[url=([^\]]+)\]([^\]]+)\[\/url\]")
  13.         strTextToReplace = regExp.Replace(strTextToReplace, "<a href=""$1"">$2</a>")
  14.  
  15.         '//Image regex
  16.         regExp = New Regex("\[img\]([^\]]+)\[\/img\]")
  17.         strTextToReplace = regExp.Replace(strTextToReplace, "<img src=""$1"" />")
  18.        
  19.         '//Bold text
  20.         regExp = New Regex("\[b\](.+?)\[\/b\]")
  21.         strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>")
  22.  
  23.         '//Italic text
  24.         regExp = New Regex("\[i\](.+?)\[\/i\]")
  25.         strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>")
  26.  
  27.         '//Underline text
  28.         regExp = New Regex("\[u\](.+?)\[\/u\]")
  29.         strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>")
  30.        
  31.         '//Font size
  32.         regExp = New Regex("\[size=([^\]]+)\]([^\]]+)\[\/size\]")
  33.         strTextToReplace = regExp.Replace(strTextToReplace, "<span style=""font-size: $1px"">$2</span>")
  34.        
  35.         '//Font color
  36.         regExp = New Regex("\[color=([^\]]+)\]([^\]]+)\[\/color\]")
  37.         strTextToReplace = regExp.Replace(strTextToReplace, "<span style=""color: $1"">$2</span>")
  38.        
  39.         Return strTextToReplace
  40.     End Function


C# Code:
Original - C# Code
  1.  
  2. public string BBCode(string strTextToReplace)
  3. {
  4.    
  5.     ////Define regex
  6.     Regex regExp;
  7.    
  8.     ////Regex for URL tag without anchor
  9.     regExp = new Regex(@"\[url\]([^\]]+)\[\/url\]");
  10.     strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>");
  11.    
  12.     ////Regex for URL with anchor
  13.     regExp = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]");
  14.     strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>");
  15.    
  16.     ////Image regex
  17.     regExp = new Regex(@"\[img\]([^\]]+)\[\/img\]");
  18.     strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />");
  19.    
  20.     ////Bold text
  21.     regExp = new Regex(@"\[b\](.+?)\[\/b\]");
  22.     strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>");
  23.    
  24.     ////Italic text
  25.     regExp = new Regex(@"\[i\](.+?)\[\/i\]");
  26.     strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>");
  27.    
  28.     ////Underline text
  29.     regExp = new Regex(@"\[u\](.+?)\[\/u\]");
  30.     strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>");
  31.    
  32.     ////Font size
  33.     regExp = new Regex(@"\[size=([^\]]+)\]([^\]]+)\[\/size\]");
  34.     strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>");
  35.    
  36.     ////Font color
  37.     regExp = new Regex(@"\[color=([^\]]+)\]([^\]]+)\[\/color\]");
  38.     strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>");               
  39.    
  40.     return strTextToReplace;
  41. }


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
File Type: zip BBCodeSample_Csharp.zip (995 Bytes, 281 views)
File Type: zip BBCodeSample_VB.zip (998 Bytes, 201 views)
__________________
jmurrayhead

Did I help you out? Make me popular by clicking the icon!

New Members:Proper way to post a question

Powered by ASP.Net

Last edited by jmurrayhead : November 15th, 2007 at 09:53 AM. Reason: added samples

Reply With Quote
  #2  
Old October 19th, 2007, 01:25 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,770 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 7 h 9 m 21 sec
Reputation Power: 469
Do you have a functioning prototype that can be tested?

Reply With Quote
  #3  
Old October 19th, 2007, 01:33 PM
jmurrayhead jmurrayhead is offline
Moderator
Click here for more information.
 
Join Date: Feb 2004
Location: Reston, VA, USA
Posts: 13,090 jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)jmurrayhead User rank is General 9th Grade (Above 100000 Reputation Level)  Folding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced FolderFolding Points: 84888 Folding Title: Advanced Folder
Time spent in forums: 3 Months 1 Week 6 h 32 m 27 sec
Reputation Power: 1580
Facebook
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

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > VB.Net/C# BBCode Function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT