最近一段时间我用vb.net做论坛,在网上找了一下,虽然有关于ubb(vb.net)的转换代码,可是都不全面,我在这里补充了一下,拿出来和大家共享。
有什么问题到我个人的论坛www.hushiyu.com来交流,随时欢迎。
Public Class ubbs
function ubb(vstr as string)
'替换HTML标记及换行处理
vstr = replace(vstr, ">", ">")
vstr = replace(vstr, "<", "<")
vstr = Replace(vstr, CHR(32), " ")
vstr = Replace(vstr, CHR(9), " ")
vstr = Replace(vstr, CHR(34), """)
vstr = Replace(vstr, CHR(39), "'")
vstr = Replace(vstr, CHR(13), "")
vstr = Replace(vstr, CHR(10), "<BR> ")
vstr = Replace(vstr, "[enter]", "<BR> ")
dim xface as integer
if xface=1 then
'表情转换,数值型变量xface=1时,允许转换
vstr = replace(vstr,":)","<img src=""em/em1.gif"">")
vstr = replace(vstr,":(","<img src=""em/em2.gif"">")
vstr = replace(vstr,":o","<img src=""em/em3.gif"">")
vstr = replace(vstr,":D","<img src=""em/em4.gif"">")
vstr = replace(vstr,";)","<img src=""em/em5.gif"">")
vstr = replace(vstr,":p","<img src=""em/em6.gif"">")
vstr = replace(vstr,":cool:","<img src=""em/em7.gif"">")
vstr = replace(vstr,":mad:","<img src=""em/em8.gif"">")
vstr = replace(vstr,":eek:","<img src=""em/em9.gif"">")
vstr = replace(vstr,":?:","<img src=""em/em0.gif"">")
end if
dim objregex as RegEx
'屏蔽JS等等
objregex = new regex("javascript")
vstr = objregex.replace(vstr,"javascript")
objregex = new regex("jscript:")
vstr = objregex.replace(vstr,"jscript:")
objregex = new regex("js:")
vstr = objregex.replace(vstr,"js:")
objregex = new regex("value")
vstr = objregex.replace(vstr,"value")
objregex = new regex("about:")
vstr = objregex.replace(vstr,"about:")
objregex = new regex("file:")
vstr = objregex.replace(vstr,"file:")
objregex = new regex("document.cookie")
vstr = objregex.replace(vstr,"documents.cookie")
objregex = new regex("vbscript:")
vstr = objregex.replace(vstr,"vbscript:")
objregex = new regex("vbs:")
vstr = objregex.replace(vstr,"vbs:")
objregex = new regex("(on(mouse|exit|error|click|key))")
vstr = objregex.replace(vstr,"on$2")
objregex = new regex("script")
vstr = objregex.replace(vstr,"script")
'UBB转换
'url
objregex = new regex("(\[URL=(.[^\[]*)\])(.*?)(\[\/URL\])")
vstr = objregex.replace(vstr,"<A HREF=""$2"" TARGET=_blank>$3</A>")
'EMAIL
objregex = new regex("\[EMAIL\](.[^\[]*)\[\/EMAIL\]")
vstr = objregex.replace(vstr,"<a href=""mailto:$1"" TARGET=""_blank"">$1</a>")
'IMG
objregex = new regex("\[IMG\](http|https|ftp):\/\/(.[^\[]*)\[\/IMG\]")
vstr = objregex.replace(vstr,"<br><a onfocus=this.blur() href=""$1://$2"" target=_blank><IMG SRC=""$1://$2"" border=0 alt=按此在新窗口浏览图片 onload=""javascript:if(this.width>screen.width-333)this.width=screen.width-333""></a>")
'自动识别URL
objregex = new regex("\[url=(http:\/\/.[^\[]*)\](.[^\[]*)(\[\/url\])")
vstr = objregex.replace(vstr,"<a href=""$1"" target=""_blank"">$2</a>")
objregex = new regex("^(http://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)")
vstr = objregex.replace(vstr,"<a href=""$1"" target=""_blank"">$1</a>")
objregex = new regex("(http://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$")
vstr = objregex.replace(vstr,"<a target=_blank href=$1>$1</a>")
objregex = new regex("[^>=""](http://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)")
vstr = objregex.replace(vstr,"<a target=_blank href=$1>$1</a>")
'COLOR
objregex = new regex("\[color=(.[^\[]*)\](.[^\[]*)\[\/color\]")
vstr = objregex.replace(vstr,"<font color=""$1"">$2</font>")
'u
objregex = new regex("\[U\](.*)\[\/U\]")
vstr = objregex.replace(vstr,"<U>$1</U>")
'B
objregex = new regex("\[B\](.*)\[\/B\]")
vstr = objregex.replace(vstr,"<B>$1</B>")
'I
objregex = new regex("\[I\](.*)\[\/I\]")
vstr = objregex.replace(vstr,"<I>$1</I>")
'FLY
objregex = new regex("\[fly\](.*)\[\/fly\]")
vstr = objregex.replace(vstr,"<marquee width=""80%"" behavior=""alternate"" scrollamount=""3"">$1</marquee>")
'SHADOW
objregex = new regex("\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/SHADOW]")
vstr = objregex.replace(vstr,"<table width=$1 style=""filter:shadow(color=$2, strength=$3)"">$4</table>")
'GLOW
objregex = new regex("\[glow=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/glow]")
vstr = objregex.replace(vstr,"<table width=$1 style=""filter:glow(color=$2, strength=$3)"">$4</table>")
'center
objregex = new regex("(\[center\])(.*?)(\[\/center\])")
vstr = objregex.replace(vstr,"<center>$2</center>")
'CODE
objregex = new regex("\[code\](.*)\[\/code\]")
vstr = objregex.replace(vstr,"<table width=""80%"" border=""0"" cellpadding=""2"" cellspacing=""0"" bgcolor=""#99FFCC"" style=""border:1px solid #000000;font-size:9pt;font-family:tahoma""><tr><td>$1</td></tr></table>")
'FLASH
objregex = new regex("(\[falsh\])(.*?)(\[\/falsh\])")
vstr = objregex.replace(vstr,"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$2""><PARAM NAME=quality VALUE=high><embed src=""$2"" quality=high pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width=500 height=400>$2</embed></OBJECT>")
objregex = new regex("(\[falsh=*([0-9]*),*([0-9]*)\])(.*?)(\[\/falsh\])")
vstr = objregex.replace(vstr,"<a href=""$4"" TARGET=_blank><IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=$2 height=$3><PARAM NAME=movie VALUE=""$4""><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=""$4"" quality=high menu=false pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width=$2 height=$3>$4</embed></OBJECT>")
'dir
objregex = new regex("\[DIR=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/DIR]")
vstr = objregex.replace(vstr,"<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>")
'rm
objregex = new regex("\[rm=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/rm]")
vstr = objregex.replace(vstr,"<OBJECT 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>")
'mp
objregex = new regex("\[mp=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/mp]")
vstr = objregex.replace(vstr,"<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=$3 width=$1 height=$2></embed></object>")
'qt
objregex = new regex("\[qt=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/qt]")
vstr = objregex.replace(vstr,"<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/>")
'QUOTE
objregex = new regex("(\[QUOTE\])(.*)(\[\/QUOTE\])")
vstr = objregex.replace(vstr,"<table cellpadding=0 cellspacing=0 border=1 WIDTH=94% bordercolor=#000000 bgcolor=#F2F8FF align=center ><tr><td ><table width=100% cellpadding=5 cellspacing=1 border=0><TR><TD >$2</table></table><br>")
'move
objregex = new regex("(\[move\])(.*)(\[\/move\])")
vstr = objregex.replace(vstr,"<MARQUEE scrollamount=3>$2</marquee>")
'size
objregex = new regex("(\[size=1\])(.[^\[]*)(\[\/size\])")
vstr = objregex.replace(vstr,"<font size=1>$2</font>")
objregex = new regex("(\[size=2\])(.[^\[]*)(\[\/size\])")
vstr = objregex.replace(vstr,"<font size=2>$2</font>")
objregex = new regex("(\[size=3\])(.[^\[]*)(\[\/size\])")
vstr = objregex.replace(vstr,"<font size=3>$2</font>")
objregex = new regex("(\[size=4\])(.[^\[]*)(\[\/size\])")
vstr = objregex.replace(vstr,"<font size=4>$2</font>")
'face
objregex = new regex("(\[face=(.[^\[]*)\])(.[^\[]*)(\[\/face\])")
vstr = objregex.replace(vstr,"<font face=$2>$3</font>")
'em
objregex = new regex("(\[em(.[^\[]*)\])")
vstr = objregex.replace(vstr,"<img src=pic\em$2.gif border=0 align=middle>")
'完成,还可以自己扩展
ubb = vstr
end function
End Class
本文地址:http://com.8s8s.com/it/it41057.htm