最近一段时间我用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