复制代码 代码如下: 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("
","<br />"); //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(),"<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 />","
")) + "</textarea>"); } 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(),"<TEXTAREA rows="12" style="width:98%" id="rid"+i+"">" + turnit(m.Groups[2].ToString().Replace("<br />","
")) + "</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 ]*?)([/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 ]*?)([/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 ]*?)([/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(@"((
)*[p])(.+?)((
)*[/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])([sS]+?)([/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 ]*?)([/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 ]+)])([ S ]*?)([/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 ]*?)([/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 ]*?)([/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 ]*?)([/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 ]+)])([ S ]*?)([/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 ]*?)([/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 ]*?)([/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 ]*?)([/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>"); }