asp.net(c#) ubb处理类
程序员文章站
2022-05-22 14:26:22
复制代码 代码如下:using system; using system.web; using system.web.ui; using system.text.regul...
复制代码 代码如下:
using system;
using system.web;
using system.web.ui;
using system.text.regularexpressions;
namespace sth.function
{
/// <summary>
/// ubbcode 的摘要说明。
/// </summary>
public class ubbcode
{
root theroot=new root();
httpcontext context = httpcontext.current;
public ubbcode()
{
//
// todo: 在此处添加构造函数逻辑
//
}
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]标签
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 全部选择 提示:你可先修改部分代码,再按运行] ");
//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>");
}
//////////////////////转换笑脸///////////////////////////
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(),"<img src='"+theroot.foderpath()+"images/face/"+ m.groups[2].tostring() +".gif'>");
}
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>");
}
//处[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>");
}
//处[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>");
}
//处标记
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>");
}
//处[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>");
}
//处[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>");
}
//处[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\"><img src=images/download.gif width=16 height=16 title='点击下载' border=0></a>");
}
//处[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='100%' height='300' style='border:1 solid #999999;margin-top:4px;margin-bottom:4px'></iframe><a href=\"" + m.groups[2].tostring() + "\" title=\"在新窗口中浏览\" target=_blank>"+m.groups[2]+"</a>");
}
//处[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>");
}
//处[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>");
}
//处[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>");
}
//处[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>");
}
//处理图片链接
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(),"<img src=\""+m.groups[2].tostring()+"\" border=\"0\" class=\"imgborder\" onload=\"if(this.width>600) {this.width=600};\" ondblclick=\"window.open('"+m.groups[2].tostring()+"')\" title=\"双击在新窗口中打开\" />");
}
else
{
sdetail = sdetail.replace(m.groups[0].tostring(),"<img src=\""+theroot.foderpath()+"drawimg.aspx?f="+m.groups[2].tostring()+"\" border=\"0\" class=\"imgborder\" onload=\"if(this.width>600) {this.width=600};\" ondblclick=\"window.open('"+m.groups[2].tostring()+"')\" title=\"双击在新窗口中打开\" />");
}
}
//处理
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>");
}
//处理[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>");
}
/////////////处理链接
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;
}
}
}
上一篇: C#编码好习惯小结
下一篇: 大数据助力智慧城市试点建设