例如:
public string Filtrate(string content)
{
if(content == null)
return "";
content = content.Replace("<"," ");
content = content.Replace(">"," ");
return(content);
}
{
if(content == null)
return "";
content = content.Replace("<"," ");
content = content.Replace(">"," ");
return(content);
}
2.UBB的显示。
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace UBB
{
/**//// <summary>
/// 功能:UBB代码
/// 作者:小迪
/// 日期:2005-3-20
/// </summary>
public class UBB
{
构造函数#region 构造函数
public UBB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
UBBToHTML#region UBBToHTML
/**//// <summary>
/// UBB代码处理函数
/// </summary>
/// <param name="detail">输入字符串</param>
/// <returns>输出字符串</returns>
public string UBBToHTML(string detail)
{
Regex r;
Match m;
处理[em]标记,处理心情图标#region 处理[em]标记,处理心情图标
string EmotPath="BBS/Skins/Default/emot/"; //路径可更改,图片名以em开头
detail = Regex.Replace(detail,@"\[em(.[^\[]*)\]",@"<img src="+EmotPath+"em$1.gif border=0 align=middle>",RegexOptions.IgnoreCase);
#endregion
处理html标记#region 处理html标记
detail = detail.Replace("<","<");
detail = detail.Replace(">",">");
detail = detail.Replace("'","''");
#endregion
处理空格#region 处理空格
detail = detail.Replace(" "," ");
#endregion
处理换行#region 处理换行
//处理换行,在每个新行的前面添加两个全角空格
r = new Regex(@"(\r\n(( )| )+)(?<正文>\S+)",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<BR> " + m.Groups["正文"].ToString());
}
//处理换行,在每个新行的前面添加两个全角空格
detail = detail.Replace("\r\n","<BR>");
//unknown
// sDetail = sDetail.Replace("\r\n","<BR><BR>");
// sDetail = sDetail.Replace("\n","<BR>");
#endregion
处理[b][/b]标记#region 处理[b][/b]标记
//方法1
r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<B>" + m.Groups[2].ToString() + "</B>");
}
//方法2
// detail = Regex.Replace(detail,@"\[b\](?<x>[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);
#endregion
处理[i][/i]标记#region 处理[i][/i]标记
//方法1
r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<I>" + m.Groups[2].ToString() + "</I>");
}
//方法2
// detail = Regex.Replace(detail,@"\[i\](?<x>[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);
#endregion
处理[U][/U]标记#region 处理[U][/U]标记
//方法1
r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<U>" + m.Groups[2].ToString() + "</U>");
}
//方法2
// detail = Regex.Replace(detail,@"\[u\](?<x>[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);
#endregion
处理[p][/p]标记#region 处理[p][/p]标记
r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])",RegexOptions.IgnoreCase|RegexOptions.Singleline);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
}
#endregion
处理[div][/div]标记#region 处理[div][/div]标记
detail = Regex.Replace(detail,@"\[center\](.[^\[]*)\[\/center\]",@"<div align=center>$1</div>",RegexOptions.IgnoreCase);
#endregion
处理[font=xxx][/font]标记#region 处理[font=xxx][/font]标记
r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<FONT FACE=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</FONT>");
}
#endregion
处理[size=xxx][/size]标记#region 处理[size=xxx][/size]标记
r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<FONT SIZE=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</FONT>");
}
#endregion
处理[color=xxx][/color]标记#region 处理[color=xxx][/color]标记
r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<FONT COLOR=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</FONT>");
}
#endregion
处理[H=x][/H]标记#region 处理[H=x][/H]标记
r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<H" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</H" + m.Groups[2].ToString() + ">");
}
#endregion
处理[fly][/fly]标记#region 处理[fly][/fly]标记
detail = Regex.Replace(detail,@"\[fly\](.[^\[]*)\[\/fly\]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
#endregion
处理[move][/move]标记#region 处理[move][/move]标记
detail = Regex.Replace(detail,@"\[move\](.[^\[]*)\[\/move\]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
#endregion
处理[GLOW][/GLOW]标记#region 处理[GLOW][/GLOW]标记
detail = Regex.Replace(detail,@"\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/GLOW\]",@"<div style=""$1px;filter:glow(color=$2, strength=$3)"">$4</div>",RegexOptions.IgnoreCase);
#endregion
处理[SHADOW][/SHADOW]标记#region 处理[SHADOW][/SHADOW]标记
detail = Regex.Replace(detail,@"\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/SHADOW\]",@"<div style=""$1px;filter:shadow(color=$2, strength=$3)"">$4</div>",RegexOptions.IgnoreCase);
#endregion
处理[sup][/sup]标记#region 处理[sup][/sup]标记
r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<SUP>" + m.Groups[2].ToString() + "</SUP>");
}
#endregion
处理[sub][/sub]标记#region 处理[sub][/sub]标记
r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<SUB>" + m.Groups[2].ToString() + "</SUB>");
}
#endregion
处理[IMG][/IMG]标记#region 处理[IMG][/IMG]标记
detail = Regex.Replace(detail,@"\[IMG\](?<x>[^\]]*)\[\/IMG\]",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);
#endregion
处理[picture][/picture]标记,处理图片链接#region 处理[picture][/picture]标记,处理图片链接
r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + m.Groups[1].ToString() +
"\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1].ToString() +
"\"></A>");
}
#endregion
处理[flash][/flash]标记#region 处理[flash][/flash]标记
detail = Regex.Replace(detail,@"\[flash\](?<x>[^\]]*)\[\/flash\]",@"<a href=""$1"" TARGET=_blank><IMG SRC=bbs/skins/default/filetype/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画! height=16 width=16>[全屏欣赏]</a><br/><OBJECT align=middle codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[flash=x,y][/flash]标记,自定义大小#region 处理[flash=x,y][/flash]标记,自定义大小
detail = Regex.Replace(detail,@"\[flash=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/flash\]",@"<a href=""$3"" TARGET=_blank><IMG SRC=bbs/skins/default/filetype/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画! height=16 width=16>[全屏欣赏]</a><br/><OBJECT align=middle codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=$1 height=$2><PARAM NAME=movie VALUE=""$3""><PARAM NAME=quality VALUE=high><embed src=""$3"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=$1 height=$2>$3</embed></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[sound][/sound]标记#region 处理[sound][/sound]标记
detail = Regex.Replace(detail,@"\[sound\](.[^\[]*)\[\/sound\]",@"<a href=""$1"" target=_blank><IMG SRC=bbs/skins/default/filetype/mid.gif border=0 alt=""背景音乐""></a><bgsound src=""$1"" loop=""-1"">",RegexOptions.IgnoreCase);
#endregion
处理[mp=x,y][/mp]标记 Dv7 MediaPlayer自定义手动播放模式;#region 处理[mp=x,y][/mp]标记 Dv7 MediaPlayer自定义手动播放模式;
detail = Regex.Replace(detail,@"\[mp=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/mp\]",@"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><param name=ShowStatusBar value=-1><param name=Filename value=$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$4 width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
#endregion
处理[mp=x,y,z][/mp]标记 Dv7 MediaPlayer自定义自动播放模式;#region 处理[mp=x,y,z][/mp]标记 Dv7 MediaPlayer自定义自动播放模式;
detail = Regex.Replace(detail,@"\[mp=*([0-9]*),*([0-9]*),*([0|1|true|false]*)\](?<x>[^\]]*)\[\/mp\]",@"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><PARAM NAME=AUTOSTART VALUE=$3 ><param name=ShowStatusBar value=-1><param name=Filename value=$4><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$4 width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
#endregion
处理[rm=x,y][/rm]标记 realplay视频文件#region 处理[rm=x,y][/rm]标记 realplay视频文件
detail = Regex.Replace(detail,@"\[rm=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/rm\]",@"<OBJECT align=middle classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br/><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[rm=x,y,z][/rm]标记 realplay视频文件#region 处理[rm=x,y,z][/rm]标记 realplay视频文件
detail = Regex.Replace(detail,@"\[rm=*([0-9]*),*([0-9]*),*([0|1|true|false]*)\](.[^\[]*)\[\/rm\]",@"<OBJECT align=middle classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$4><PARAM NAME=CONSOLE VALUE=""Clip""><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=$3 ></OBJECT><br/><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$4><PARAM NAME=AUTOSTART VALUE=$3><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=""Clip""></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[QT=x,y][/QT]标记 QuickTime视频文件#region 处理[QT=x,y][/QT]标记 QuickTime视频文件
detail = Regex.Replace(detail,@"\[QT=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/QT\]",@"<embed src=$3 width=$1 height=$2 autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>",RegexOptions.IgnoreCase);
#endregion
处理[list=xxx][/list]标记#region 处理[list=xxx][/list]标记
r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); 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]);
}
detail = detail.Replace(m.Groups[0].ToString(),
"<UL TYPE=\"" + m.Groups[3].ToString() + "\"><B>" + m.Groups[4].ToString() + "</B>" +
strLI + "</UL>");
}
//unknown
// detail = Regex.Replace(detail,@"\[list\](?<x>[^\]]*)\[/list\]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);
// detail = Regex.Replace(detail,@"\[list=1\](?<x>[^\]]*)\[/list\]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);
// detail = Regex.Replace(detail,@"\[list=a\](?<x>[^\]]*)\[/list\]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);
// detail = Regex.Replace(detail,@"\[\*\](?<x>[^\]]*)\[/\*\]",@"<li>$1</li>",RegexOptions.IgnoreCase);
#endregion
处理[center][/center]标记#region 处理[center][/center]标记
detail = Regex.Replace(detail,@"\[center\](?<x>[^\]]*)\[\/center\]",@"<div align=center>$1</div>",RegexOptions.IgnoreCase);
#endregion
处理[align=xxx][/align]标记#region 处理[align=xxx][/align]标记
r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<P align=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</P>");
}
#endregion
处理[url][/url]标记#region 处理[url][/url]标记
//方法1
r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/url.gif\">" +
m.Groups[2].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[URL\](?<x>[^\]]*)\[\/URL\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
#endregion
处理[url=xxx][/url]标记#region 处理[url=xxx][/url]标记
//方法1
r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/url.gif\">" +
m.Groups[3].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[URL=(?<x>[^\]]*)\](?<y>[^\]]*)\[\/URL\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
#endregion
处理[email][/email]标记#region 处理[email][/email]标记
//方法1
r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/email.gif\">" +
m.Groups[2].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[EMAIL\](?<x>[^\]]*)\[\/EMAIL\]",@"<img align=middle src=bbs/skins/default/email1.gif><A HREF=""mailto:$1"">$1</A>",RegexOptions.IgnoreCase);
#endregion
处理[email=xxx][/email]标记#region 处理[email=xxx][/email]标记
//方法1
r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/email.gif\">" +
m.Groups[3].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[EMAIL=(\S+\@.[^\[]*)\](?<y>[^\]]*)\[\/EMAIL\]",@"<img align=middle src=bbs/skins/default/email1.gif><A HREF=""mailto:$1"" TARGET=_blank>$2</A>",RegexOptions.IgnoreCase);
#endregion
处理[quote][/quote]标记#region 处理[quote][/quote]标记
detail = Regex.Replace(detail,@"\[quote](?<x>.*)\[/quote]",@"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0'><tr><td>$1</td></tr></table></center>",RegexOptions.IgnoreCase);
#endregion
处理[code][/code]标记#region 处理[code][/code]标记
detail = Regex.Replace(detail,@"\[code\](?<x>[^\]]*)\[/code\]",@"<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""6""><td><b>以下内容为程序代码:</b><br/>$1</td></table>",RegexOptions.IgnoreCase);
#endregion
处理[HTML][/HTML]标记#region 处理[HTML][/HTML]标记
detail = Regex.Replace(detail,@"\[HTML\](?<x>[^\]]*)\[\/HTML\]",@"table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""6""><td><b>以下内容为程序代码:</b><br/>$1</td></table>",RegexOptions.IgnoreCase);
#endregion
处理[DIR=x,y][/DIR]#region 处理[DIR=x,y][/DIR]
detail = Regex.Replace(detail,@"\[DIR=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/DIR\]",@"<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src value=$3><embed src=$3 pluginspage=http://www.macromedia.com/shockwave/download/ width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
#endregion
return detail;
// return "<P Style=\"FONT-FAMILY: 宋体;LINE-HEIGHT: 13pt;word-wrap:break-word;\">" + sDetail + "</P>";
}
#endregion
}
}
using System.Text;
using System.Text.RegularExpressions;
namespace UBB
{
/**//// <summary>
/// 功能:UBB代码
/// 作者:小迪
/// 日期:2005-3-20
/// </summary>
public class UBB
{
构造函数#region 构造函数
public UBB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
UBBToHTML#region UBBToHTML
/**//// <summary>
/// UBB代码处理函数
/// </summary>
/// <param name="detail">输入字符串</param>
/// <returns>输出字符串</returns>
public string UBBToHTML(string detail)
{
Regex r;
Match m;
处理[em]标记,处理心情图标#region 处理[em]标记,处理心情图标
string EmotPath="BBS/Skins/Default/emot/"; //路径可更改,图片名以em开头
detail = Regex.Replace(detail,@"\[em(.[^\[]*)\]",@"<img src="+EmotPath+"em$1.gif border=0 align=middle>",RegexOptions.IgnoreCase);
#endregion
处理html标记#region 处理html标记
detail = detail.Replace("<","<");
detail = detail.Replace(">",">");
detail = detail.Replace("'","''");
#endregion
处理空格#region 处理空格
detail = detail.Replace(" "," ");
#endregion
处理换行#region 处理换行
//处理换行,在每个新行的前面添加两个全角空格
r = new Regex(@"(\r\n(( )| )+)(?<正文>\S+)",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<BR> " + m.Groups["正文"].ToString());
}
//处理换行,在每个新行的前面添加两个全角空格
detail = detail.Replace("\r\n","<BR>");
//unknown
// sDetail = sDetail.Replace("\r\n","<BR><BR>");
// sDetail = sDetail.Replace("\n","<BR>");
#endregion
处理[b][/b]标记#region 处理[b][/b]标记
//方法1
r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<B>" + m.Groups[2].ToString() + "</B>");
}
//方法2
// detail = Regex.Replace(detail,@"\[b\](?<x>[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);
#endregion
处理[i][/i]标记#region 处理[i][/i]标记
//方法1
r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<I>" + m.Groups[2].ToString() + "</I>");
}
//方法2
// detail = Regex.Replace(detail,@"\[i\](?<x>[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);
#endregion
处理[U][/U]标记#region 处理[U][/U]标记
//方法1
r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<U>" + m.Groups[2].ToString() + "</U>");
}
//方法2
// detail = Regex.Replace(detail,@"\[u\](?<x>[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);
#endregion
处理[p][/p]标记#region 处理[p][/p]标记
r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])",RegexOptions.IgnoreCase|RegexOptions.Singleline);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
}
#endregion
处理[div][/div]标记#region 处理[div][/div]标记
detail = Regex.Replace(detail,@"\[center\](.[^\[]*)\[\/center\]",@"<div align=center>$1</div>",RegexOptions.IgnoreCase);
#endregion
处理[font=xxx][/font]标记#region 处理[font=xxx][/font]标记
r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<FONT FACE=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</FONT>");
}
#endregion
处理[size=xxx][/size]标记#region 处理[size=xxx][/size]标记
r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<FONT SIZE=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</FONT>");
}
#endregion
处理[color=xxx][/color]标记#region 处理[color=xxx][/color]标记
r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<FONT COLOR=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</FONT>");
}
#endregion
处理[H=x][/H]标记#region 处理[H=x][/H]标记
r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<H" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</H" + m.Groups[2].ToString() + ">");
}
#endregion
处理[fly][/fly]标记#region 处理[fly][/fly]标记
detail = Regex.Replace(detail,@"\[fly\](.[^\[]*)\[\/fly\]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
#endregion
处理[move][/move]标记#region 处理[move][/move]标记
detail = Regex.Replace(detail,@"\[move\](.[^\[]*)\[\/move\]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
#endregion
处理[GLOW][/GLOW]标记#region 处理[GLOW][/GLOW]标记
detail = Regex.Replace(detail,@"\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/GLOW\]",@"<div style=""$1px;filter:glow(color=$2, strength=$3)"">$4</div>",RegexOptions.IgnoreCase);
#endregion
处理[SHADOW][/SHADOW]标记#region 处理[SHADOW][/SHADOW]标记
detail = Regex.Replace(detail,@"\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/SHADOW\]",@"<div style=""$1px;filter:shadow(color=$2, strength=$3)"">$4</div>",RegexOptions.IgnoreCase);
#endregion
处理[sup][/sup]标记#region 处理[sup][/sup]标记
r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<SUP>" + m.Groups[2].ToString() + "</SUP>");
}
#endregion
处理[sub][/sub]标记#region 处理[sub][/sub]标记
r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),"<SUB>" + m.Groups[2].ToString() + "</SUB>");
}
#endregion
处理[IMG][/IMG]标记#region 处理[IMG][/IMG]标记
detail = Regex.Replace(detail,@"\[IMG\](?<x>[^\]]*)\[\/IMG\]",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);
#endregion
处理[picture][/picture]标记,处理图片链接#region 处理[picture][/picture]标记,处理图片链接
r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + m.Groups[1].ToString() +
"\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1].ToString() +
"\"></A>");
}
#endregion
处理[flash][/flash]标记#region 处理[flash][/flash]标记
detail = Regex.Replace(detail,@"\[flash\](?<x>[^\]]*)\[\/flash\]",@"<a href=""$1"" TARGET=_blank><IMG SRC=bbs/skins/default/filetype/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画! height=16 width=16>[全屏欣赏]</a><br/><OBJECT align=middle codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[flash=x,y][/flash]标记,自定义大小#region 处理[flash=x,y][/flash]标记,自定义大小
detail = Regex.Replace(detail,@"\[flash=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/flash\]",@"<a href=""$3"" TARGET=_blank><IMG SRC=bbs/skins/default/filetype/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画! height=16 width=16>[全屏欣赏]</a><br/><OBJECT align=middle codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=$1 height=$2><PARAM NAME=movie VALUE=""$3""><PARAM NAME=quality VALUE=high><embed src=""$3"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=$1 height=$2>$3</embed></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[sound][/sound]标记#region 处理[sound][/sound]标记
detail = Regex.Replace(detail,@"\[sound\](.[^\[]*)\[\/sound\]",@"<a href=""$1"" target=_blank><IMG SRC=bbs/skins/default/filetype/mid.gif border=0 alt=""背景音乐""></a><bgsound src=""$1"" loop=""-1"">",RegexOptions.IgnoreCase);
#endregion
处理[mp=x,y][/mp]标记 Dv7 MediaPlayer自定义手动播放模式;#region 处理[mp=x,y][/mp]标记 Dv7 MediaPlayer自定义手动播放模式;
detail = Regex.Replace(detail,@"\[mp=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/mp\]",@"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><param name=ShowStatusBar value=-1><param name=Filename value=$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$4 width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
#endregion
处理[mp=x,y,z][/mp]标记 Dv7 MediaPlayer自定义自动播放模式;#region 处理[mp=x,y,z][/mp]标记 Dv7 MediaPlayer自定义自动播放模式;
detail = Regex.Replace(detail,@"\[mp=*([0-9]*),*([0-9]*),*([0|1|true|false]*)\](?<x>[^\]]*)\[\/mp\]",@"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><PARAM NAME=AUTOSTART VALUE=$3 ><param name=ShowStatusBar value=-1><param name=Filename value=$4><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$4 width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
#endregion
处理[rm=x,y][/rm]标记 realplay视频文件#region 处理[rm=x,y][/rm]标记 realplay视频文件
detail = Regex.Replace(detail,@"\[rm=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/rm\]",@"<OBJECT align=middle classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br/><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[rm=x,y,z][/rm]标记 realplay视频文件#region 处理[rm=x,y,z][/rm]标记 realplay视频文件
detail = Regex.Replace(detail,@"\[rm=*([0-9]*),*([0-9]*),*([0|1|true|false]*)\](.[^\[]*)\[\/rm\]",@"<OBJECT align=middle classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$4><PARAM NAME=CONSOLE VALUE=""Clip""><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=$3 ></OBJECT><br/><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$4><PARAM NAME=AUTOSTART VALUE=$3><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=""Clip""></OBJECT>",RegexOptions.IgnoreCase);
#endregion
处理[QT=x,y][/QT]标记 QuickTime视频文件#region 处理[QT=x,y][/QT]标记 QuickTime视频文件
detail = Regex.Replace(detail,@"\[QT=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/QT\]",@"<embed src=$3 width=$1 height=$2 autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>",RegexOptions.IgnoreCase);
#endregion
处理[list=xxx][/list]标记#region 处理[list=xxx][/list]标记
r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); 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]);
}
detail = detail.Replace(m.Groups[0].ToString(),
"<UL TYPE=\"" + m.Groups[3].ToString() + "\"><B>" + m.Groups[4].ToString() + "</B>" +
strLI + "</UL>");
}
//unknown
// detail = Regex.Replace(detail,@"\[list\](?<x>[^\]]*)\[/list\]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);
// detail = Regex.Replace(detail,@"\[list=1\](?<x>[^\]]*)\[/list\]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);
// detail = Regex.Replace(detail,@"\[list=a\](?<x>[^\]]*)\[/list\]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);
// detail = Regex.Replace(detail,@"\[\*\](?<x>[^\]]*)\[/\*\]",@"<li>$1</li>",RegexOptions.IgnoreCase);
#endregion
处理[center][/center]标记#region 处理[center][/center]标记
detail = Regex.Replace(detail,@"\[center\](?<x>[^\]]*)\[\/center\]",@"<div align=center>$1</div>",RegexOptions.IgnoreCase);
#endregion
处理[align=xxx][/align]标记#region 处理[align=xxx][/align]标记
r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<P align=" + m.Groups[2].ToString() + ">" +
m.Groups[3].ToString() + "</P>");
}
#endregion
处理[url][/url]标记#region 处理[url][/url]标记
//方法1
r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/url.gif\">" +
m.Groups[2].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[URL\](?<x>[^\]]*)\[\/URL\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
#endregion
处理[url=xxx][/url]标记#region 处理[url=xxx][/url]标记
//方法1
r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/url.gif\">" +
m.Groups[3].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[URL=(?<x>[^\]]*)\](?<y>[^\]]*)\[\/URL\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
#endregion
处理[email][/email]标记#region 处理[email][/email]标记
//方法1
r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/email.gif\">" +
m.Groups[2].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[EMAIL\](?<x>[^\]]*)\[\/EMAIL\]",@"<img align=middle src=bbs/skins/default/email1.gif><A HREF=""mailto:$1"">$1</A>",RegexOptions.IgnoreCase);
#endregion
处理[email=xxx][/email]标记#region 处理[email=xxx][/email]标记
//方法1
r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase);
for (m = r.Match(detail); m.Success; m = m.NextMatch())
{
detail = detail.Replace(m.Groups[0].ToString(),
"<A href=\"mailto:" + m.Groups[2].ToString() + "\" target=\"_blank\"><IMG border=0 src=\"images/email.gif\">" +
m.Groups[3].ToString() + "</A>");
}
//方法2
// detail = Regex.Replace(detail,@"\[EMAIL=(\S+\@.[^\[]*)\](?<y>[^\]]*)\[\/EMAIL\]",@"<img align=middle src=bbs/skins/default/email1.gif><A HREF=""mailto:$1"" TARGET=_blank>$2</A>",RegexOptions.IgnoreCase);
#endregion
处理[quote][/quote]标记#region 处理[quote][/quote]标记
detail = Regex.Replace(detail,@"\[quote](?<x>.*)\[/quote]",@"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0'><tr><td>$1</td></tr></table></center>",RegexOptions.IgnoreCase);
#endregion
处理[code][/code]标记#region 处理[code][/code]标记
detail = Regex.Replace(detail,@"\[code\](?<x>[^\]]*)\[/code\]",@"<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""6""><td><b>以下内容为程序代码:</b><br/>$1</td></table>",RegexOptions.IgnoreCase);
#endregion
处理[HTML][/HTML]标记#region 处理[HTML][/HTML]标记
detail = Regex.Replace(detail,@"\[HTML\](?<x>[^\]]*)\[\/HTML\]",@"table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""6""><td><b>以下内容为程序代码:</b><br/>$1</td></table>",RegexOptions.IgnoreCase);
#endregion
处理[DIR=x,y][/DIR]#region 处理[DIR=x,y][/DIR]
detail = Regex.Replace(detail,@"\[DIR=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/DIR\]",@"<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src value=$3><embed src=$3 pluginspage=http://www.macromedia.com/shockwave/download/ width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
#endregion
return detail;
// return "<P Style=\"FONT-FAMILY: 宋体;LINE-HEIGHT: 13pt;word-wrap:break-word;\">" + sDetail + "</P>";
}
#endregion
}
}
补:
处理[upload]标记,处理上传图片#region 处理[upload]标记,处理上传图片
string UpLoadPath="UpLoadPic/"; //路径可更改,图片名以upload_开头
detail = Regex.Replace(detail,@"\[upload_(.[^\[]*)\]",@"<img src="+UpLoadPath+"upload_$1 border=0 align=middle>",RegexOptions.IgnoreCase);
#endregion
string UpLoadPath="UpLoadPic/"; //路径可更改,图片名以upload_开头
detail = Regex.Replace(detail,@"\[upload_(.[^\[]*)\]",@"<img src="+UpLoadPath+"upload_$1 border=0 align=middle>",RegexOptions.IgnoreCase);
#endregion
其实是上传任何文件,如果只要传图片,则可以在程序中加以控制(如,控制后缀名)
改进:
处理[upload]标记,处理上传图片#region 处理[upload]标记,处理上传图片
string UpLoadPath="UpLoadPic/"; //路径可更改,图片名以upload_开头
detail = Regex.Replace(detail,@"\[upload_(.[^\[]*)_(\d{1,2}|[1-2]\d{2}|3[0-1]\d|320)_([\d]+)\.(\w+)\]",@"<a href='" + UpLoadPath + "upload_$1_$2_$3.$4'><img src="+UpLoadPath+"upload_$1_$2_$3.$4 border=0 align=middle></a>",RegexOptions.IgnoreCase);
detail = Regex.Replace(detail,@"\[upload_(.[^\[]*)_(3[2-9]\d|[4-9]\d{2}|\d{4,})_([\d]+)\.(\w+)\]",@"<a href='" + UpLoadPath + "upload_$1_$2_$3.$4'><img src="+UpLoadPath+"upload_$1_$2_$3.$4 border=0 align=middle width=320></a>",RegexOptions.IgnoreCase);
#endregion
string UpLoadPath="UpLoadPic/"; //路径可更改,图片名以upload_开头
detail = Regex.Replace(detail,@"\[upload_(.[^\[]*)_(\d{1,2}|[1-2]\d{2}|3[0-1]\d|320)_([\d]+)\.(\w+)\]",@"<a href='" + UpLoadPath + "upload_$1_$2_$3.$4'><img src="+UpLoadPath+"upload_$1_$2_$3.$4 border=0 align=middle></a>",RegexOptions.IgnoreCase);
detail = Regex.Replace(detail,@"\[upload_(.[^\[]*)_(3[2-9]\d|[4-9]\d{2}|\d{4,})_([\d]+)\.(\w+)\]",@"<a href='" + UpLoadPath + "upload_$1_$2_$3.$4'><img src="+UpLoadPath+"upload_$1_$2_$3.$4 border=0 align=middle width=320></a>",RegexOptions.IgnoreCase);
#endregion
相关文件:
private void ibtnUpLoad_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(upLoadFile.PostedFile.ContentLength > 524288)
{
this.Response.Write("<script>alert('图片请控制在500K以内!谢谢')</script>");
return;
}
HttpPostedFile hpPFile = upLoadFile.PostedFile;
string localFile = hpPFile.FileName;
//设置路径
string strFolderName=Server.MapPath("UpLoadPic");
//取得文件类型(后缀名)
char[] separator2 = {'.'}; //separator的值为"\"
string[] temp2 = localFile.Split(separator2);
string strFileType = temp2[temp2.Length-1];
//取得文件名(不含路径)
char separator3 = '.';
string strTemp = localFile.Substring(0,localFile.LastIndexOf(separator3)-1);
char[] separator = {'\\'}; //separator的值为"\"
string[] temp = strTemp.Split(separator);
string strFileName = temp[temp.Length-1];
System.IO.FileStream fs = new FileStream(localFile,FileMode.Open);
Bitmap bit = new Bitmap(fs);
int picWidth = bit.Width;
int picHeight = bit.Height;
fs.Close();
string FileName = strFolderName + "\\upload_" + strFileName + "_" + picWidth + "_" + picHeight + "." + strFileType;
if(strFileType.ToLower() == "gif" || strFileType.ToLower() == "jpg")
{
try
{
hpPFile.SaveAs(FileName);
content.Value += "[upload_" + strFileName + "_" + picWidth + "_" + picHeight + "." + strFileType + "]";
this.Response.Write("<script>alert('上传成功!')</script>");
}
catch
{
this.Response.Write("<script>alert('上传失败!请重试!')</script>");
}
}
else
{
this.Response.Write("<script>alert('请选择有效的图片文件,目前只支持.gif和.jpg格式的图片!')</script>");
}
}