asp.net(c#) ubb处理类
本文导语: asp.net下对于编辑器中的ubb进行替换处理的代码。 代码如下: using System; using System.Web; using System.Web.UI; using System.Text.RegularExpressions; namespace STH.function { /// /// UbbCode 的摘要说明。 /// public class UbbCode { Root theroot=new Root()...
asp.net下对于编辑器中的ubb进行替换处理的代码。
using System;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
namespace STH.function
{
///
/// UbbCode 的摘要说明。
///
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","
");
//sDetail = sDetail.Replace(" "," ");
//[code]标签
r = new Regex(@"([code])([sS]+?)([/code])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"" + turnit(m.Groups[2].ToString().Replace("
","n")) + "");
}
int i=1;
r = new Regex(@"([html])([sS]+?)([/html])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
i=i+1;
sDetail = sDetail.Replace(m.Groups[0].ToString(),"" + turnit(m.Groups[2].ToString().Replace("
","n")) + " [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行] ");
//sDetail = sDetail.Replace("
","");
}
sDetail=turnhtm(sDetail);
return turnof(sDetail);
}
public String turnhtm(string sDetail)
{
Regex r;
Match m;
//////////[b][/b]
r = new Regex(@"([b])([ St]*?)([/b])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"" + m.Groups[2].ToString() + "");
}
//////////////////////转换笑脸///////////////////////////
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])([ St]*?)([/i])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"" + m.Groups[2].ToString() + "");
}
r = new Regex(@"([U])([ St]*?)([/U])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"" + m.Groups[2].ToString() + "");
}
//处[p][/p]标记
r = new Regex(@"((rn)*[p])(.+?)((rn)*[/p])",RegexOptions.IgnoreCase|RegexOptions.Singleline);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"
" + m.Groups[3].ToString() + "
");}
//处[quote][/quote]标记
r = new Regex(@"([quote])([sS]+?)([/quote])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"Quote:
" + m.Groups[2].ToString() + "
");}
//处标记
r = new Regex(@"([url])([ St]*?)([/url])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[2].ToString() + "");
}
//处[url=xxx][/url]标记
r = new Regex(@"([url=([ St]+)])([ St]*?)([/url])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[3].ToString() + "");
}
//处[email][/email]标记
r = new Regex(@"([email])([ St]*?)([/email])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[2].ToString() + "");
}
//处[down][/down]标记
r = new Regex(@"([down])([ St]*?)([/down])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"");
}
//处[w][/w]标记
r = new Regex(@"([w])([ St]*?)([/w])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
""+m.Groups[2]+"");
}
//处[email=xxx][/email]标记
r = new Regex(@"([email=([ St]+)])([ St]*?)([/email])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[3].ToString() + "");
}
//处[size=x][/size]标记
r = new Regex(@"([size=([1-7])])([ St]*?)([/size])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[3].ToString() + "");
}
//处[color=x][/color]标记
r = new Regex(@"([color=([S]+)])([ St]*?)([/color])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[3].ToString() + "");
}
//处[font=x][/font]标记
r = new Regex(@"([font=(w+)])([ St]*?)([/font])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"" +
m.Groups[3].ToString() + "");
}
//处理图片链接
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(),"");
}
else
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"");
}
}
//处理
r = new Regex(@"([align=(w+)])([ St]*?)([/align])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
" +
m.Groups[3].ToString() + "
}
//处理[list=x][*][/list]
r = new Regex(@"([list(=(A|a|I|i| ))?]([ St]*)rn)(([*]([ St]*rn))*?)([/list])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
string strLI = m.Groups[5].ToString();
Regex rLI = new Regex(@"[*]([ St]*rn?)",RegexOptions.IgnoreCase);
Match mLI;
for (mLI = rLI.Match(strLI); mLI.Success; mLI = mLI.NextMatch())
{
strLI = strLI.Replace(mLI.Groups[0].ToString(),"
}
sDetail = sDetail.Replace(m.Groups[0].ToString(),
"
- " + m.Groups[4].ToString() + "" +
strLI + "
}
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(),"");
}
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(),"
");
}
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(),"");
}
/////////////处理链接
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]+"" + m.Groups[2].ToString() + "");
}
r = new Regex(@"(^|s|
|
)((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]+"" + m.Groups[2].ToString() + "");
}
return sDetail;
}
}
}