Copy Code code as follows:
Using System;
Using System.Web;
Using System.Web.UI;
Using System.Text.RegularExpressions;
Namespace Sth.function
{
<summary>
Summary description of the Ubbcode.
</summary>
public class Ubbcode
{
Root theroot=new root ();
HttpContext context = HttpContext.Current;
Public Ubbcode ()
{
//
TODO: Add 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] Label
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 ALL SELECT hint: You can modify some of the code first, and then 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 Smiley///////////////////////////
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>");
}
Place [p][/p] Mark
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>");
}
Place [Quote][/quote] Mark
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>");
}
Place marked
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>");
}
Place [Url=xxx][/url] Mark
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>");
}
Place [Email][/email] Mark
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>");
}
Place [Down][/down] Mark
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> ");
}
Place [w][/w] Mark
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= ' 100% ' height= ' style= ' border:1 solid #999999; margin-top:4px;margin-bottom : 4px ' ></iframe><a href=\ ' + m.groups[2]. ToString () + "\ title=\" Browse in a new window \ "target=_blank>" +m.groups[2]+ "</A>");
}
Place [Email=xxx][/email] Mark
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>");
}
Place [Size=x][/size] Mark
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>");
}
Place [Color=x][/color] Mark
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>");
}
Place [Font=x][/font] Mark
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>");
}
Working with picture 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 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 New window \"/> ");
}
}
Processing
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>");
}
processing [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>");
}
Working with links
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;
}
}
}