最近我又开始用c#做论坛,在网上找了一下,虽然也有关于ubb(c#)的转换代码,可是同样都不全面,我在这里补充了一下,拿出来和大家共享。
有什么问题到我个人的论坛[url=http://www.hushiyu.com]www.hushiyu.com[/url]来交流,随时欢迎。
using system;
using system.text;
using system.text.regularexpressions;
namespace myluntan
{
/// <summary>
/// ubb 的摘要说明。
/// </summary>
public class ubb
{
public ubb()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region 公共静态方法
/// <summary>
/// ubb代码处理函数
/// </summary>
/// <param name=”sdetail”>输入字符串</param>
/// <returns>输出字符串</returns>
public string ubbtohtml(string sdetail)
{
regex r;
match m;
#region 处理空格
sdetail = sdetail.replace(” “,” ”);
#endregion
#region 处理单引号
sdetail = sdetail.replace(“”,”’”);
#endregion
#region 处理双引号
sdetail = sdetail.replace(“\””,”"”);
#endregion
#region html标记符
sdetail = sdetail.replace(“<“,”<”);
sdetail = sdetail.replace(“>”,”>”);
#endregion
#region 处理换行
//处理换行,在每个新行的前面添加两个全角空格
r = new regex(@”(\r\n(( )| )+)(?<正文> +)”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<br> ” + m.groups[“正文”].tostring());
}
//处理换行,在每个新行的前面添加两个全角空格
sdetail = sdetail.replace(“\r\n”,”<br>”);
#endregion
#region 处[b][/b]标记
r = new regex(@”(\[b\])([ \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>”);
}
#endregion
#region 处[i][/i]标记
r = new regex(@”(\[i\])([ \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>”);
}
#endregion
#region 处[u][/u]标记
r = new regex(@”(\[u\])([ \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>”);
}
#endregion
#region 处[p][/p]标记
//处[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>”);
}
#endregion
#region 处[sup][/sup]标记
//处[sup][/sup]标记
r = new regex(@”(\[sup\])([ \t]*?)(\[\/sup\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<sup>” + m.groups[2].tostring() + “</sup>”);
}
#endregion
#region 处[sub][/sub]标记
//处[sub][/sub]标记
r = new regex(@”(\[sub\])([ \t]*?)(\[\/sub\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<sub>” + m.groups[2].tostring() + “</sub>”);
}
#endregion
#region 处[url][/url]标记
//处[url][/url]标记
r = new regex(@”(\[url\])([ \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>”);
}
#endregion
#region 处[url=xxx][/url]标记
//处[url=xxx][/url]标记
r = new regex(@”(\[url=([ \t]+)\])([ \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>”);
}
#endregion
#region 处[email][/email]标记
//处[email][/email]标记
r = new regex(@”(\[email\])([ \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>”);
}
#endregion
#region 处[email=xxx][/email]标记
//处[email=xxx][/email]标记
r = new regex(@”(\[email=([ \t]+)\])([ \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>”);
}
#endregion
#region 处[size=x][/size]标记
//处[size=x][/size]标记
r = new regex(@”(\[size=([1-7])\])([ \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>”);
}
#endregion
#region 处[color=x][/color]标记
//处[color=x][/color]标记
r = new regex(@”(\[color=([ ]+)\])([ \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>”);
}
#endregion
#region 处[font=x][/font]标记
//处[font=x][/font]标记
r = new regex(@”(\[font=([ ]+)\])([ \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>”);
}
#endregion
#region 处理图片链接
//处理图片链接
r = new regex(“\\[picture\\](\\d+?)\\[\\/picture\\]”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<a href=\”showimage.aspx?type=all&action=forumimage&imageid=” + m.groups[1].tostring() +
“\” target=\”_blank\”><img border=0 title=\”点击打开新窗口查看\” src=\”showimage.aspx?action=forumimage&imageid=” + m.groups[1].tostring() +
“\”></a>”);
}
#endregion
#region 处理[align=x][/align]
//处理[align=x][/align]
r = new regex(@”(\[align=([ ]+)\])([ \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>”);
}
#endregion
#region 处[h=x][/h]标记
//处[h=x][/h]标记
r = new regex(@”(\[h=([1-6])\])([ \t]*?)(\[\/h\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<h” + m.groups[2].tostring() + “>” +
m.groups[3].tostring() + “</h” + m.groups[2].tostring() + “>”);
}
#endregion
#region 处理[list=x][*][/list]
//处理[list=x][*][/list]
r = new regex(@”(\[list(=(a|a|i|i| ))?\]([ \t]*)\r\n)((\[\*\]([ \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(@”\[\*\]([ \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>”);
}
#endregion
#region 处[shadow=x][/shadow]标记
//处[shadow=x][/shadow]标记
r = new regex(@”(\[shadow=)(\d*?),(#*\w*?),(\d*?)\]([ \t]*?)(\[\/shadow\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<table width=” + m.groups[2].tostring() + ” style=filter:shadow(color=” + m.groups[3].tostring() + “, strength=” + m.groups[4].tostring() + “)>” +
m.groups[5].tostring() + “</table>”);
}
#endregion
#region 处[glow=x][/glow]标记
//处[glow=x][/glow]标记
r = new regex(@”(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([ \t]*?)(\[\/glow\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<table width=” + m.groups[2].tostring() + ” style=filter:glow(color=” + m.groups[3].tostring() + “, strength=” + m.groups[4].tostring() + “)>” +
m.groups[5].tostring() + “</table>”);
}
#endregion
#region 处[center][/center]标记
r = new regex(@”(\[center\])([ \t]*?)(\[\/center\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<center>” + m.groups[2].tostring() + “</center>”);
}
#endregion
#region 处[img][/img]标记
r = new regex(@”(\[img\])(http|https|ftp):\/\/([ \t]*?)(\[\/img\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<br><a onfocus=this.blur() href=” + m.groups[2].tostring() + “://” + m.groups[3].tostring() + ” target=_blank><img src=” + m.groups[2].tostring() + “://” + m.groups[3].tostring() + ” border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>”);
}
#endregion
#region 处[em]标记
r = new regex(@”(\[em([ \t]*?)\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<img src=pic/em” + m.groups[2].tostring() + “.gif border=0 align=middle>”);
}
#endregion
#region 处[flash=x][/flash]标记
//处[mp=x][/mp]标记
r = new regex(@”(\[flash=)(\d*?),(\d*?)\]([ \t]*?)(\[\/flash\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<a href=” + m.groups[4].tostring() + ” target=_blank><img src=pic/swf.gif border=0 alt=点击开新窗口欣赏该flash动画!> [全屏欣赏]</a><br><br><object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:d27cdb6e-ae6d-11cf-96b8-444553540000 width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + “><param name=movie value=” + m.groups[4].tostring() + “><param name=quality value=high><param name=menu value=false><embed src=” + m.groups[4].tostring() + ” quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + “>” + m.groups[4].tostring() + “</embed></object>”);
}
#endregion
#region 处[dir=x][/dir]标记
//处[dir=x][/dir]标记
r = new regex(@”(\[dir=)(\d*?),(\d*?)\]([ \t]*?)(\[\/dir\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<object classid=clsid:166b1bca-3f9c-11cf-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + “><param name=src value=” + m.groups[4].tostring() + “><embed src=” + m.groups[4].tostring() + ” pluginspage=http://www.macromedia.com/shockwave/download/ width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + “></embed></object>”);
}
#endregion
#region 处[rm=x][/rm]标记
//处[rm=x][/rm]标记
r = new regex(@”(\[rm=)(\d*?),(\d*?)\]([ \t]*?)(\[\/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[2].tostring() + ” height=” + m.groups[3].tostring() + “><param name=src value=” + m.groups[4].tostring() + “><param name=console value=clip1><param name=controls value=imagewindow><param name=autostart value=true></object><br><object classid=clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa height=32 id=video2 width=” + m.groups[2].tostring() + “><param name=src value=” + m.groups[4].tostring() + “><param name=autostart value=-1><param name=controls value=controlpanel><param name=console value=clip1></object>”);
}
#endregion
#region 处[mp=x][/mp]标记
//处[mp=x][/mp]标记
r = new regex(@”(\[mp=)(\d*?),(\d*?)\]([ \t]*?)(\[\/mp\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<object align=middle classid=clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=object id=mediaplayer width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + ” ><param name=showstatusbar value=-1><param name=filename value=” + m.groups[4].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[4].tostring() + ” width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + “></embed></object>”);
}
#endregion
#region 处[qt=x][/qt]标记
//处[qt=x][/qt]标记
r = new regex(@”(\[qt=)(\d*?),(\d*?)\]([ \t]*?)(\[\/qt\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<embed src=” + m.groups[4].tostring() + ” width=” + m.groups[2].tostring() + ” height=” + m.groups[3].tostring() + ” autoplay=true loop=false controller=true playeveryframe=false cache=false scale=tofit bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>”);
}
#endregion
#region 处[quote][/quote]标记
r = new regex(@”(\[quote\])([ \t]*?)(\[\/quote\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<table cellpadding=0 cellspacing=0 border=1 width=94% bordercolor=#000000 bgcolor=#f2f8ff align=center style=font-size: 9pt><tr><td ><table width=100% cellpadding=5 cellspacing=1 border=0><tr><td >” + m.groups[2].tostring() + “</table></table><br>”);
}
#endregion
#region 处[move][/move]标记
r = new regex(@”(\[move\])([ \t]*?)(\[\/move\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<marquee scrollamount=3>” + m.groups[2].tostring() + “</marquee>”);
}
#endregion
#region 处[fly][/fly]标记
r = new regex(@”(\[fly\])([ \t]*?)(\[\/fly\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),”<marquee width=80% behavior=alternate scrollamount=3>” + m.groups[2].tostring() + “</marquee>”);
}
#endregion
#region 处[image][/image]标记
//处[image][/image]标记
r = new regex(@”(\[image\])([ \t]*?)(\[\/image\])”,regexoptions.ignorecase);
for (m = r.match(sdetail); m.success; m = m.nextmatch())
{
sdetail = sdetail.replace(m.groups[0].tostring(),
“<img src=\”” + m.groups[2].tostring() + “\” border=0 align=middle><br>”);
}
#endregion
return sdetail;
}
#endregion
}
}