asp.net(c#) ubb處理類
更新時(shí)間:2008年08月30日 08:35:41 作者:
asp.net下對(duì)于編輯器中的ubb進(jìn)行替換處理的實(shí)現(xiàn)代碼
復(fù)制代碼 代碼如下:
using System;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;
namespace STH.function
{
/// <summary>
/// UbbCode 的摘要說(shuō)明。
/// </summary>
public class UbbCode
{
Root theroot=new Root();
HttpContext context = HttpContext.Current;
public UbbCode()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
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]標(biāo)簽
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 全部選擇 提示:你可先修改部分代碼,再按運(yùn)行] ");
//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>");
}
//////////////////////轉(zhuǎn)換笑臉///////////////////////////
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]標(biāo)記
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]標(biāo)記
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>");
}
//處標(biāo)記
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]標(biāo)記
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]標(biāo)記
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]標(biāo)記
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='點(diǎn)擊下載' border=0></A>");
}
//處[w][/w]標(biāo)記
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]標(biāo)記
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]標(biāo)記
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]標(biāo)記
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]標(biāo)記
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=\"雙擊在新窗口中打開(kāi)\" />");
}
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=\"雙擊在新窗口中打開(kāi)\" />");
}
}
//處理
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#獲取網(wǎng)頁(yè)HTML源碼實(shí)例
- C#實(shí)現(xiàn)壓縮HTML代碼的方法
- C#實(shí)現(xiàn)過(guò)濾html標(biāo)簽并保留a標(biāo)簽的方法
- 使用C#獲取網(wǎng)頁(yè)HTML源碼的例子
- C#實(shí)現(xiàn)將PPT轉(zhuǎn)換成HTML的方法
- C#正則實(shí)現(xiàn)Ubb解析類的代碼
- C#將jpg轉(zhuǎn)換為pdf的方法
- C#引用類型轉(zhuǎn)換的常見(jiàn)方式總結(jié)
- 教你如何用C#制作文字轉(zhuǎn)換成聲音程序
- C#實(shí)現(xiàn)HTML和UBB互相轉(zhuǎn)換的方法
相關(guān)文章
asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例
asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例,需要的朋友可以參考一下2013-04-04
ASP.NET設(shè)計(jì)FTP文件上傳的解決方案
這篇文章主要介紹了ASP.NET設(shè)計(jì)FTP文件上傳的解決方案,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09
asp.net Repeater 數(shù)據(jù)綁定代碼
asp.net Repeater 數(shù)據(jù)綁定代碼2010-03-03
Repeater綁定dictionary數(shù)據(jù)源代碼及報(bào)錯(cuò)解決
為大家講解下Repeater綁定dictionary數(shù)據(jù)源以及報(bào)錯(cuò)處理的方法,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04
通過(guò)HttpClient 調(diào)用ASP.NET Web API示例
本篇文章主要介紹了通過(guò)HttpClient 調(diào)用ASP.NET Web API示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
asp.net core調(diào)用wps實(shí)現(xiàn)word轉(zhuǎn)pdf的過(guò)程
這篇文章主要介紹了asp.net core調(diào)用wps實(shí)現(xiàn)word轉(zhuǎn)pdf的過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-08-08
VS2015 Update2 構(gòu)建 Android 程序問(wèn)題匯總
這篇文章主要介紹了VS2015 Update2 構(gòu)建 Android 程序問(wèn)題匯總的相關(guān)資料,需要的朋友可以參考下2016-07-07
詳解如何在ASP.Net Core中實(shí)現(xiàn)健康檢查
這篇文章主要介紹了詳解如何在ASP.Net Core中實(shí)現(xiàn)健康檢查,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

