/// <summary>
/// UBB 转换函数。
/// </summary>
public class UBB
{
#region Fields
private static string iconPath = string.Empty;
#endregion #region Methods
/// <summary>
/// 将UBB文本内容转换成HTML代码内容。
/// </summary>
/// <param name="text">待转换的UBB文本内容</param>
/// <returns>转换后的HTML代码内容</returns>
public static string UBBToHtml(string text)
{
if(text == null)
return ""; text = text.Replace(" ","&nbsp;");
text = text.Replace("<","&lt");
text = text.Replace(">","&gt");
text = text.Replace("\n","<br>");
text = text.Replace("\r\n","<br>");
//处理
text = Regex.Replace(text,@"\[URL\](?<x>[^\]]*)\[\/URL\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
//处理x
text = Regex.Replace(text,@"\[URL=(?<x>[^\]]*)\](?<y>[^\]]*)\[\/URL\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
//处理[EMAIL][/EMAIL]
text = Regex.Replace(text,@"\[EMAIL\](?<x>[^\]]*)\[\/EMAIL\]",@"<img align=middle src=bbs/skins/default/email1.gif><A HREF=""mailto:$1"">$1</A>",RegexOptions.IgnoreCase);
//处理[EMAIL=x][/EMAIL]
text = Regex.Replace(text,@"\[EMAIL=(\S+\@.[^\[]*)\](?<y>[^\]]*)\[\/EMAIL\]",@"<img align=middle src=bbs/skins/default/email1.gif><A HREF=""mailto:$1"" TARGET=_blank>$2</A>",RegexOptions.IgnoreCase);
//处理心情图标
text = Regex.Replace(text,@"\[em(.[^\[]*)\]",@"<img src=" + iconPath + "em$1.gif border=0 align=middle>",RegexOptions.IgnoreCase);
//处理[div][/div]
text = Regex.Replace(text,@"\[center\](.[^\[]*)\[\/center\]",@"<div align=center>$1</div>",RegexOptions.IgnoreCase);
//处理[fly][/fly]
text = Regex.Replace(text,@"\[fly\](.[^\[]*)\[\/fly\]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
//处理[move][/move]
text = Regex.Replace(text,@"\[move\](.[^\[]*)\[\/move\]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
//处理[SHADOW][/SHADOW]
text = Regex.Replace(text,@"\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/SHADOW\]",@"<div style=""width:$1px;filter:shadow(color=$2, strength=$3)"">$4</div>",RegexOptions.IgnoreCase);
//处理[GLOW][/GLOW]
text = Regex.Replace(text,@"\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/GLOW\]",@"<div style=""width:$1px;filter:glow(color=$2, strength=$3)"">$4</div>",RegexOptions.IgnoreCase);
//处理
text = Regex.Replace(text,@"\[b\](?<x>[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);
//处理
text = Regex.Replace(text,@"\[i\](?<x>[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);
//处理
text = Regex.Replace(text,@"\[u\](?<x>[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);
//处理[center][/center]
text = Regex.Replace(text,@"\[center\](?<x>[^\]]*)\[\/center\]",@"<div align=center>$1</div>",RegexOptions.IgnoreCase);
text = Regex.Replace(text,@"\[list\](?<x>[^\]]*)\[/list\]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);
text = Regex.Replace(text,@"\[list=1\](?<x>[^\]]*)\[/list\]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);        
text = Regex.Replace(text,@"\[list=a\](?<x>[^\]]*)\[/list\]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);
text = Regex.Replace(text,@"\[\*\](?<x>[^\]]*)\[/\*\]",@"<li>$1</li>",RegexOptions.IgnoreCase);
text = Regex.Replace(text,@"\",@"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0'><tr><td>$1</td></tr></table></center>",RegexOptions.IgnoreCase);
//处理[code][/code]
text = Regex.Replace(text,@"\[code\](?<x>[^\]]*)\[/code\]",@"<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""6""><td><b>以下内容为程序代码:</b><br/>$1</td></table>",RegexOptions.IgnoreCase);
//处理[HTML][/HTML]
text = Regex.Replace(text,@"\[HTML\](?<x>[^\]]*)\[\/HTML\]",@"table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""6""><td><b>以下内容为程序代码:</b><br/>$1</td></table>",RegexOptions.IgnoreCase);
//处理[IMG][/IMG]
text = Regex.Replace(text,@"\[IMG\](?<x>[^\]]*)\[\/IMG\]",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);
//处理[flash][/flash]
text = Regex.Replace(text,@"\[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);
//处理[flash=x,y][/flash]自定义大小
text = Regex.Replace(text,@"\[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);
//处理[sound][/sound]
text = Regex.Replace(text,@"\[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);
//处理[mp=x,y][/mp]Dv7 MediaPlayer自定义手动播放模式;
text = Regex.Replace(text,@"\[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);

解决方案 »

  1.   

    //处理[mp=x,y,z][/mp]Dv7 MediaPlayer自定义自动播放模式;
    text = Regex.Replace(text,@"\[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);
    //处理[rm=x,y][/rm]
    text = Regex.Replace(text,@"\[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);
    //处理[rm=x,y,z][/u]
    text = Regex.Replace(text,@"\[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);
    //处理[DIR=x,y][/DIR]
    text = Regex.Replace(text,@"\[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);
    //处理[QT=x,y][/QT]
    text = Regex.Replace(text,@"\[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); return(text);
    }
    #endregion #region Properties
    /// <summary>
    /// UBB代码里心情图标存放的相对路径地址。[默认为空]
    /// </summary>
    public static string IconPath
    {
    get{return iconPath;}
    set{iconPath = value;}
    }
    #endregion }