转一篇 ASP.NET UBB代码的转换方式
什么是 UBB 标记?
UBB 标记是您可能已经熟悉的 HTML标记的一种变种。基本上,它允许您在文章中添加本来需要 HTML 标记的功能或风格。即使您所在的讨论区不允许使用 HTML 标记,UBB 标记仍然可以使用。因为比 HTML 要求更少的编码技术,您也许更愿意使用 UBB 标记,即便讨论区允许使用 HTML 标记。
目前支持的 UBB 标记:
UltraBoard Code 说明
[url]www.bbs.com[/url] 加入带有说明的超级联结。加入带有说明的超级联结。加入带有说明的超级联结。
[url=www.bbs.com]www.bbs.com[/url]
[url=http://www.bbs.com]www.bbs.com[/url]
[download=http://www.bbs.com/2.zip]下载[/download] 下载地址和说明
[email]blue_dream@netease.com[/email] 加入带有说明的电子邮件地址。
[img]Your image address[/img] 显示你的图像,但请记住,http://是必须的。
[b]粗体[/b] 粗体显示
[i]斜体[/i] 斜体显示
[u]下划线[/u] 带有下划线的显示
[1]字号一[/1] 字号一
[2]字号二[/2] 字号二
[3]字号三[/3] 字号三
[4]字号四[/4] 字号四
[sup]上标[/sup] 上标显示
[sub]下标[/sub] 下标显示
[center]居中[/center] 居中显示
[right]居右[/right] 居右显示
[color=#0000FF]颜色[/color] 带有颜色的显示
[&] 显示符号 &
[*]条目 无序缩进和列表
[list]条目 条目
[quote]引用[/quote] 引用显示
[fly]滚动文本[/fly] 相当于html marquee tag,其中direction为right,behavior为scroll,scrollamount为10,scrolldelay为200
[font=宋体]字体[/font] 字体
[size=3]字体大小[/size] 字体大小
[#f7f7f7]彩色[/#] 同[color]标记
[code]代码[/code] 代码显示
[html]调试代码[/html] 发帖后,对于中间的调试代码,可以点运行按钮,查看调试效果
附注
您不能在一个功能中混合使用 HTML 和 UBB 标记。同时 UBB 标记是大小写无关的 (因而,你可以使用 [URL] 或 [url]).
不正确的 UBB 标记用法:
不支持嵌套。
[url="www.bbs.com"]www.bbs.com[/url] - 等于号后加的内容不用加引号。
[url] www.my.com [/url] - 不要在标记和标记作用的文字之间加空格。
[email]home@my.com[email] - 结束标记必须包括正斜杠 ([/email])
ASP.NET UBB实现代码:
private string HTMLEncode(string str)
{
str = str.Replace(">", ">");
str = str.Replace("<", "<");
char ch;
ch=(char)32;
str = str.Replace(ch.ToString(), " ");
ch=(char)34;
str = str.Replace(ch.ToString(), """);
ch=(char)39;
str = str.Replace(ch.ToString(), "'");
ch=(char)13;
str = str.Replace(ch.ToString(), "");
ch=(char)10;
str = str.Replace(ch.ToString(), "<BR>
");
return str;
}
public string Ubbcode(string chr)
{
if(chr==null)
return "";
chr=chr.Replace("<","<");
chr=chr.Replace(">",">");
chr=chr.Replace("\n","<br>
");
chr = Regex.Replace(chr,@"<script(?
<x>
[^\>]*)>(?
<y>
[^\>]*) \</script\>",@"<script$1>$2</script>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[url=(?
<x>
[^\]]*)\](?
<y>
[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[url\](?
<x>
[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[email=(?
<x>
[^\]]*)\](?
<y>
[^\]]*)\[/email\]",@"<a href=$1>$2</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[email\](?
<x>
[^\]]*)\[/email\]",@"<a href=$1>$1</a>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[flash](?
<x>
[^\]]*)\[/flash]",@"
<OBJECT 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);
chr = Regex.Replace(chr,@"\[img](?
<x>
[^\]]*)\[/img]",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[color=(?
<x>
[^\]]*)\](?
<y>
[^\]]*)\[/color\]",@"<font color=$1>$2</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[face=(?
<x>
[^\]]*)\](?
<y>
[^\]]*)\[/face\]",@"<font face=$1>$2</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=1\](?
<x>
[^\]]*)\[/size\]",@"<font size=1>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=2\](?
<x>
[^\]]*)\[/size\]",@"<font size=2>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=3\](?
<x>
[^\]]*)\[/size\]",@"<font size=3>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[size=4\](?
<x>
[^\]]*)\[/size\]",@"<font size=4>$1</font>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[align=(?
<x>
[^\]]*)\](?
<y>
[^\]]*)\[/align\]",@"<align=$1>$2
</align>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[fly](?
<x>
[^\]]*)\[/fly]",@"
<marquee width=90% behavior=alternate scrollamount=3>
$1
</marquee>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[move](?
<x>
[^\]]*)\[/move]",@"
<marquee scrollamount=3>
$1
</marquee>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[glow=(?
<x>
[^\]]*),(?
<y>
[^\]]*),(?
<z>
[^\]]*)\](?
<w>
[^\]]*)\[/glow\]",@"
<table width=$1 style=filter:glow(color=$2, strength=$3)>
$4
</table>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[shadow=(?
<x>
[^\]]*),(?
<y>
[^\]]*),(?
<z>
[^\]]*)\](?
<w>
[^\]]*)\[/shadow\]",@"
<table width=$1 style=filter:shadow(color=$2, strength=$3)>
$4
</table>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[b\](?
<x>
[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[i\](?
<x>
[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[u\](?
<x>
[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[code\](?
<x>
[^\]]*)\[/code\]",@"
<pre id=code><font size=1 face=Verdana, Arial id=code>$1</font id=code></pre id=code>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[list\](?
<x>
[^\]]*)\[/list\]",@"
<ul>
$1
</ul>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[list=1\](?
<x>
[^\]]*)\[/list\]",@"
<ol type=1>
$1
</ol id=1>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[list=a\](?
<x>
[^\]]*)\[/list\]",@"
<ol type=a>
$1
</ol id=a>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[\*\](?
<x>
[^\]]*)\[/\*\]",@"
<li>$1</li>
",RegexOptions.IgnoreCase);
chr = Regex.Replace(chr,@"\[quote](?
<x>
.*)\[/quote]",@"
<center>
-- 以下是引用 --
<table border=1 width=80% cellpadding=10 cellspacing=0 >
<tr>
<td>$1</td>
</tr>
</table>
</center>
",RegexOptions.IgnoreCase);
return(chr);
}

浙公网安备 33010602011771号