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();
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\])([\s\S]+?)(\[\/code\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"");
}
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()," [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\])([ \S\t]*?)(\[\/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\])([ \S\t]*?)(\[\/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\])([ \S\t]*?)(\[\/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(@"((\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(),"

" + m.Groups[3].ToString() + "

");
}

//处[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:

" + m.Groups[2].ToString() + "

");
}


//处标记
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(),
"" +
m.Groups[2].ToString() + "
");
}

//处[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(),
"" +
m.Groups[3].ToString() + "
");
}

//处[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(),
"" +
m.Groups[2].ToString() + "
");
}
//处[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(),
"");
}
//处[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(),
""+m.Groups[2]+"");
}

//处[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(),
"" +
m.Groups[3].ToString() + "
");
}

//处[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(),
"" +
m.Groups[3].ToString() + "
");
}

//处[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(),
"" +
m.Groups[3].ToString() + "
");
}

//处[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(),
"" +
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(),"600) {this.width=600};\" ondblclick=\"window.open('"+m.Groups[2].ToString()+"')\" title=\"双击在新窗口中打开\" />");
}
else
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"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(),
"

" +
m.Groups[3].ToString() + "

");
}




//处理[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(),"
  • " + mLI.Groups[1]);
    }
    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;
    }
    }
    }

  • 以上就是asp.net(c#) ubb处理类的详细内容,更多请关注0133技术站其它相关文章!

    赞(0) 打赏
    未经允许不得转载:0133技术站首页 » ASP编程