Copy codeThe Code is as follows: using System;
Using System. Web;
Using System. Web. UI;
Using System. Text. RegularExpressions;
Namespace… function
{
/// <Summary>
/// Summary of UbbCode.
/// </Summary>
Public class UbbCode
{
Root theroot = new Root ();
HttpContext context = HttpContext. Current;
Public UbbCode ()
{
//
// TODO: add the constructor logic here
//
}
Public String unhtml (string str)
{
Str = context. Server. HtmlEncode (str );
Str = str. Replace ("&","&");
Return str;
}
Public String turnit (string str)
{
Regex r;
Match m;
Str = str. Replace ("[", "[| ");
Str = str. Replace ("]", "|]");
R = new Regex (@ "(http | mms | rtsp | ftp | https )(:\/\/)");
For (m = r. Match (str); m. Success; m = m. NextMatch ())
{
Str = str. replace (m. groups [0]. toString (), m. groups [1]. toString () + "$" + m. groups [2]. toString ());
}
Return str;
}
Public String turnof (string str)
{
Regex r;
Match m;
Str = str. Replace ("[| ","[");
Str = str. Replace ("|]", "]");
R = new Regex (@ "(http | mms | rtsp | ftp | https) (\ $ :\/\/)");
For (m = r. Match (str); m. Success; m = m. NextMatch ())
{
Str = str. replace (m. groups [0]. toString (), m. groups [1]. toString () + m. groups [2]. toString (). replace ("$ ",""));
}
Return str;
}
Public String UBB (string sDetail)
{
Regex r;
Match m;
SDetail = sDetail. Replace ("[swf]", "[swf = 300,250]");
SDetail = sDetail. Replace ("[rm]", "[rm = 300,250]");
SDetail = sDetail. Replace ("[mp]", "[mp = 300,250]");
SDetail = unhtml (sDetail );
SDetail = sDetail. Replace ("\ n", "<br/> ");
// SDetail = sDetail. Replace ("","");
// [Code] tag
R = new Regex (@ "(\ [code \]) ([\ s \ S] + ?) (\ [\/Code \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<textarea style = \" border: 1px solid #94BBE2; width: 98%; OVERFLOW: hidden; cursor: default; \ "rows = \" 15 \ "onclick = \" this. style. posHeight = this. scrollHeight \ ">" + turnit (m. groups [2]. toString (). replace ("<br/>", "\ n") + "</textarea> ");
}
Int I = 1;
R = new Regex (@ "(\ [html \]) ([\ s \ S] + ?) (\ [\/Html \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
I = I + 1;
SDetail = sDetail. replace (m. groups [0]. toString (), "<TEXTAREA rows = \" 12 \ "style = \" width: 98% \ "id = \" rid "+ I +" \ ">" + turnit (m. groups [2]. toString (). replace ("<br/>", "\ n") + "</TEXTAREA> <INPUT onclick = \" runEx ('rid "+ I + "') \ "type = button value = \" Have A Try \ "class = \" input1 \ "> [Ctrl + A select all tips: you can modify some code first, then press run] ");
// SDetail = sDetail. Replace ("<br/> ","");
}
SDetail = turnhtm (sDetail );
Return turnof (sDetail );
}
Public String turnhtm (string sDetail)
{
Regex r;
Match m;
/// // [B] [/B]
R = new Regex (@ "(\ [B \]) ([\ S \ t] *?) (\ [\/B \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<B>" + m. groups [2]. toString () + "</B> ");
}
//// // Convert the smiling face //////////////// ///////////
R = new Regex (@ "(\ [face =) ([0-9] *) \]", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), " ");
}
R = new Regex (@ "(\ [I \]) ([\ S \ t] *?) (\ [\/I \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<I>" + m. groups [2]. toString () + "</I> ");
}
R = new Regex (@ "(\ [U \]) ([\ S \ t] *?) (\ [\/U \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<U>" + m. groups [2]. toString () + "</U> ");
}
// Mark [p] [/p]
R = new Regex (@ "(\ r \ n) * \ [p \]) (. + ?) (\ R \ n) * \ [\/p \]) ", RegexOptions. IgnoreCase | RegexOptions. Singleline );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<P class = \" pstyle \ ">" + m. groups [3]. toString () + "</P> ");
}
// Mark [quote] [/quote]
R = new Regex (@ "(\ [quote \]) ([\ s \ S] + ?) (\ [\/Quote \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "Quote: <br/> <p class = \" qstyle \ ">" + m. groups [2]. toString () + "</p> ");
}
// Mark
R = new Regex (@ "(\ [url \]) ([\ S \ t] *?) (\ [\/Url \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<A href = \" "+ m. Groups [2]. ToString () +" \ "target = \" _ blank \ ">" +
M. Groups [2]. ToString () + "</A> ");
}
// Mark [url = xxx] [/url]
R = new Regex (@ "(\ [url = ([\ S \ t] +) \]) ([\ S \ t] *?) (\ [\/Url \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<A href = \" "+ m. Groups [2]. ToString () +" \ "target = \" _ blank \ ">" +
M. Groups [3]. ToString () + "</A> ");
}
// Mark [email] [/email]
R = new Regex (@ "(\ [email \]) ([\ S \ t] *?) (\ [\/Email \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<A href = \" mailto: "+ m. Groups [2]. ToString () +" \ "target = \" _ blank \ ">" +
M. Groups [2]. ToString () + "</A> ");
}
// Mark [down] [/down]
R = new Regex (@ "(\ [down \]) ([\ S \ t] *?) (\ [\/Down \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<A href = \" "+ m. groups [2]. toString () + "\" target = \ "_ blank \"> </A> ");
}
// Mark [w] [/w]
R = new Regex (@ "(\ [w \]) ([\ S \ t] *?) (\ [\/W \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<Iframe src = \" "+ m. groups [2]. toString () + "\" frameborder = '0' width = '000000' height = '000000' style = 'border: 1 solid # 100%; margin-top: 4px; margin-bottom: 4px '> </iframe> <a href = \ "" + m. groups [2]. toString () + "\" title = \ "browse in new window \" target = _ blank> "+ m. groups [2] + "</A> ");
}
// Mark [email = xxx] [/email]
R = new Regex (@ "(\ [email = ([\ S \ t] +) \]) ([\ S \ t] *?) (\ [\/Email \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<A href = \" mailto: "+ m. Groups [2]. ToString () +" \ "target = \" _ blank \ ">" +
M. Groups [3]. ToString () + "</A> ");
}
// Mark [size = x] [/size]
R = new Regex (@ "(\ [size = ([1-7]) \]) ([\ S \ t] *?) (\ [\/Size \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<Font size =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</FONT> ");
}
// Mark [color = x] [/color]
R = new Regex (@ "(\ [color = ([\ S] +) \]) ([\ S \ t] *?) (\ [\/Color \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<Font color =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</FONT> ");
}
// Mark [font = x] [/font]
R = new Regex (@ "(\ [font = (\ w +) \]) ([\ S \ t] *?) (\ [\/Font \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<Font face =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</FONT> ");
}
// Process image links
R = new Regex (@ "(\ [img \]) (. + ?) (\ [\/Img \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
If (m. Groups [0]. ToString (). IndexOf ("http: //")> = 0)
{
SDetail = sDetail. replace (m. groups [0]. toString (), " 600) {this. width = 600 };\ "ondblclick = \" window. open ('"+ m. groups [2]. toString () + "') \" title = \ "double-click to open \"/> "in a new window ");
}
Else
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (), " 600) {this. width = 600 };\ "ondblclick = \" window. open ('"+ m. groups [2]. toString () + "') \" title = \ "double-click to open \"/> "in a new window ");
}
}
// Process
R = new Regex (@ "(\ [align = (\ w +) \]) ([\ S \ t] *?) (\ [\/Align \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<P align =" + m. Groups [2]. ToString () + ">" +
M. Groups [3]. ToString () + "</P> ");
}
// Process [list = x] [*] [/list]
R = new Regex (@ "(\ [list (= (A | a | I | I | ))? \] ([\ S \ t] *) \ r \ n) (\ [\ * \] ([\ S \ t] * \ r \ n )) *?) (\ [\/List \]) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
String strLI = m. Groups [5]. ToString ();
Regex rLI = new Regex (@ "\ [\ * \] ([\ S \ t] * \ r \ n ?) ", RegexOptions. IgnoreCase );
Match mLI;
For (mLI = rLI. Match (strLI); mLI. Success; mLI = mLI. NextMatch ())
{
StrLI = strLI. Replace (mLI. Groups [0]. ToString (), "<LI>" + mLI. Groups [1]);
}
SDetail = sDetail. Replace (m. Groups [0]. ToString (),
"<Ul type = \" "+ m. groups [3]. toString () + "\"> <B> "+ m. groups [4]. toString () + "</B>" +
StrLI + "</UL> ");
}
R = new Regex (@ "\ [swf = ([0-9] *), ([0-9] *) \] (. + ?) \ [\/Swf \] ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<object classid = clsid: D27CDB6E-AE6D-11cf-96B8-444553540000 codebase = http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0 width =" + m. groups [1]. toString () + "height =" + m. groups [2]. toString () + "> <param name = movie value = '" + m. groups [3]. toString () + "'> <param name = quality value = high> <embed src ='" + m. groups [3]. toString () + "' Quality = high pluginspage = http://www.macromedia.com/shockwave/download/index.cgi? P1_Prod_Version = ShockwaveFlash type = application/x-shockwave-flash width = "+ m. groups [1]. toString () + "height =" + m. groups [2]. toString () + "> </embed> </object> ");
}
R = new Regex (@ "\ [rm = ([0-9] *), ([0-9] *) \] (. + ?) \ [\/Rm \] ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<OBJECT classid = clsid: CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class = OBJECT id = RAOCX width =" + m. groups [1]. toString () + "height =" + m. groups [2]. toString () + "> <param name = src value = '" + m. groups [3]. toString () + "'> <param name = console value = Clip1> <param name = controls value = imagewindow> <param name = autostart value = false> <param name = loop VALUE = true> </OBJECT> <br/> <OBJECT classid = CLSID: CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height = 32 id = video2 width = "+ m. groups [1]. toString () + "> <param name = src value = '" + m. groups [3]. toString () + "'> <param name = autostart value = false> <param name = loop VALUE = true> <param name = controls value = controlpanel> <param name = console value = Clip1> </OBJECT> ");
}
R = new Regex (@ "\ [mp = ([0-9] *), ([0-9] *) \] (. + ?) \ [\/Mp \] ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), "<object classid = 'clsid: 22d6f312-b0f6-11d0-94ab-0080c74c7e95 'class = 'object' id = MediaPlayer width =" + m. groups [1]. toString () + "height =" + m. groups [2]. toString () + "> <param name = ShowStatusBar value =-1> <param name = autostart value = false> <param name = loop value = true> <param name = Filename value = '" + m. groups [3]. toString () + "'> <embed type = application/x-oleobject codebase = http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename = mp src ='" + m. groups [2]. toString () + "'width =" + m. groups [1]. toString () + "height =" + m. groups [2]. toString () + "> </embed> </object> ");
}
//// // Process the link
R = new Regex (@ "([^> = ""\? \ ']) (Http | mms | rtsp | ftp | https): // ([A-Za-z0-9 \. \/= \? % \-&_~ '@': +!] +) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), m. groups [1] + "<a href = '" + m. groups [2]. toString () + "'target = _ blank>" + m. groups [2]. toString () + "</a> ");
}
R = new Regex (@ "(^ | \ s | <br/> | <p>) (http | https | ftp | rtsp | mms )(: \ //) ([A-Za-z0-9 \. \/ = \? % \-&_~ '@': +!] +) ", RegexOptions. IgnoreCase );
For (m = r. Match (sDetail); m. Success; m = m. NextMatch ())
{
SDetail = sDetail. replace (m. groups [0]. toString (), m. groups [1] + "<a href = '" + m. groups [2]. toString () + "'target = _ blank>" + m. groups [2]. toString () + "</a> ");
}
Return sDetail;
}
}
}