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