(写于2003年6月)
此段代码部分是从网上其他资料里获得,然后我加以修改及完善,用了比较笨拙的办法replace(嘻嘻,自己还没掌握正则表达式)实现了UBB 代码,因为一直觉得这种方法是不是比较笨,所以不好意思贴出来,今天有网友用到,想对初学者也有价值,因而将源码全部公布出来,供参考,因为时间关系,没有任何注解,我会逐渐完善。
该段ubb代码实现功能
[B][/B] 加粗,替换为html标记<b></b>
[I][/I] 斜体,替换为html标记<i></i>
[U][/U] 加下划线,替换为html标记<u></u>
[br] 换行,替换为html标记<br>
[COLOR][/COLOR] 文本颜色,替换为html标记<font color=xxx></font>。
用法:[COLOR=xxx]文本[/COLOR]。
[MYCODE][/MYCODE] 保留源码输入格式,替换为html标记<pre></pre>。其中的文本会完全保留,不会被UBB替换
[URL][/URL] 超连接,替换为html标记<a></a>。
用法:[URL=连接地址]显示的文本[/URL](长格式)或[URL]连接地址[/URL](短格式),注意不要在“连接”的两端加引号。
[EMAIL][/EMAIL] email地址,替换为html标记<a href=mailto:xxx></a>。
用法:[EMAIL=邮箱地址]人名[/EMAIL](长格式)或[EMAIL]邮箱地址[/EMAIL](短格式)。其中的“地址”必须是合法的email格式。
[IMAGE][/IMAGE] 图片,替换为html标记<img src="xxx" border=0>。
用法:[IMAGE]图片地址[/IMAGE]。其中的“地址”必须是图片的完整路径。
[SOUND][/SOUND] 多媒体声音,可以把歌曲的url放在sound后。
用法:[SOUND=歌曲地址]歌名[/SOUND]。
[FLASH][/FLASH] FLASH,可以把FLASH的url放在flash后。
用法:[FLASH=flash地址]FLASH名[/FLASH]。
代码如下:
对要实现进行UBB的文本,只需调用icode2html()就可以了
yourtext="......" '你的文本,含有UBB标记
icode2html(yourtext,1,1) '后面两个参数是表示是否禁止image,是否禁止sound及flash
<%
Function icode2Html(str,unimage,unsound)
if not str<>"" then exit function str=HTMLEncode(str) str=replace(str,chr(13)+chr(10),"<br>") str=replace(str,chr(32)," ") tmpstr="icode" str=icodeStr2(str,"mycode") str=icodeStr2(str,"url") str=icodeStr2(str,"email") if not unimage then str=icodeStr2(str,"image") str=icodeStr1(str,"url") str=icodeStr1(str,"email") str=icodeStr1(str,"color") if not unsound then str=icodeStr1(str,"sound")
if not unsound then str=icodeStr1(str,"flash")
str=replace(str,"[b]","<b>",1,-1,1) str=replace(str,"[/b]","</b>",1,-1,1)
str=replace(str,"[i]","<i>",1,-1,1) str=replace(str,"[/i]","</i>",1,-1,1)
str=replace(str,"[u]","<u>",1,-1,1) str=replace(str,"[/u]","</u>",1,-1,1)
str=replace(str,"[br]","<br>",1,-1,1) str=replace(str,"["&tmpstr,"[",1,-1,1) str=replace(str,tmpstr&"]","]",1,-1,1)
str=replace(str,"/"&tmpstr,"/",1,-1,1)
icode2Html=str End Function
function icodeStr1(icode_str,icodeKeyWord)
beginstr=1 endstr=1
do while icodeKeyWord="url" or icodeKeyWord="email" or icodeKeyWord="color" or icodeKeyWord="sound" or icodeKeyWord="flash"
beginstr=instr(beginstr,icode_str,"["&icodeKeyWord&"=",1)
if beginstr=0 then exit do endstr=instr(beginstr,icode_str,"]",1) if endstr=0 then exit do laststr=instr(beginstr,icode_str,"[/"&icodeKeyWord&"]",1)
if laststr=0 then exit do LicodeKeyWord=icodeKeyWord beginstr=beginstr+len(LicodeKeyWord)+2 text=mid(icode_str,beginstr,endstr-beginstr) if icodeKeyWord="flash" then
swf=instr(1,text,".swf",1)
if swf=0 then exit do
end if
if icodeKeyWord="sound" then
mp3=instr(1,text,".mp3",1)
wav=instr(1,text,".wav",1)
wma=instr(1,text,".wma",1)
asf=instr(1,text,".asf",1)
midi=instr(1,text,".mid",1)
if mp3=0 and wav=0 and wma=0 and asf=0 and midi=0 then exit do
end if
select case icodeKeyWord
case "url"
icode_str=replace(icode_str,"[url="&text&"]","<a href='"&text&"' target='_blank'>",1,1,1) icode_str=replace(icode_str,"[/url]","</a>",1,1,1) case "email" icode_str=replace(icode_str,"[email="&text&"]","<a href='mailto:"&text&"'>",1,1,1)
icode_str=replace(icode_str,"[/email]","</a>",1,1,1)
case "color"
icode_str=replace(icode_str,"[color="&text&"]","<font color='"&text&"'>",1,1,1)
icode_str=replace(icode_str,"[/color]","</font>",1,1,1)
case "sound"
icode_str=replace(icode_str,"[sound="&text&"]","<embed width='70' height='25' autostart='0' loop='0' src='"&text&"'> <a href='"&text&"' target='_blank'> 下载 </a>",1,1,1)
icode_str=replace(icode_str,"[/sound]","<br>",1,1,1)
case "flash"
icode_str=replace(icode_str,"[flash="&text&"]","<embed width='160' height='160' play='0' loop='0' src='"&text&"'><br><br> 画面点右键选择PLAY播放 <a href='"&text&"' target='_blank'>开新窗口欣赏</a>",1,1,1)
icode_str=replace(icode_str,"[/flash]","<br>",1,1,1)
end select
loop icodestr1=icode_str end function
function icodeStr2(icode_str,icodeKeyWord)
tmpstr="icode" 此变量将用于[MYCODE]xxxxxx[/MYCODE]中,处理文本xxxxxx
beginstr=1 endstr=1
do while icodeKeyWord="url" or icodeKeyWord="email" or icodeKeyWord="image" or icodeKeyWord="mycode"
beginstr=instr(beginstr,icode_str,"["&icodeKeyWord&"]",1)
if beginstr=0 then exit do
endstr=instr(beginstr,icode_str,"[/"&icodeKeyWord&"]",1)
if endstr=0 then exit do
LicodeKeyWord=icodeKeyWord
beginstr=beginstr+len(licodeKeyWord)+2
text=mid(icode_str,beginstr,endstr-beginstr)
if icodeKeyWord="image" then
gif=instr(1,text,".gif",1)
jpg=instr(1,text,".jpg",1)
if gif=0 and jpg=0 then exit do
end if
select case icodeKeyWord
case "url"
icode_str=replace(icode_str,"[url]"&text,"<a href='"&text&"' target='_blank'>"&text,1,1,1)
icode_str=replace(icode_str,"[/url]","</a>",1,1,1)
case "email"
icode_str=replace(icode_str,"[email]"&text," <a href='mailto:"&text&"'>"&text,1,1,1)
icode_str=replace(icode_str,"[/email]","</a>",1,1,1)
case "image"
icode_str=replace(icode_str,"[image]"&text,"<table width='100%' align=center border='0' cellspacing='0' cellpadding='0' style='table-layout: fixed'><tr><td><a href='"&text&"' target=_blank><img src="&text,1,1,1)
icode_str=replace(icode_str,"[/image]"," border=0 alt='点击打开新窗口'></a></td></tr></table>",1,1,1)
case "mycode"
codetext=replace(text,"[","["&tmpstr,1,-1,1)
codetext=replace(codetext,"]",tmpstr&"]",1,-1,1)
codetext=replace(codetext,"/","/"&tmpstr,1,-1,1)
icode_str=replace(icode_str,"[mycode]"&text,"<pre style='position:relative;color: #003366; background-color: #C5CFDC;padding:10 10 10 10;margin:15'>"&codetext,1,1,1)
icode_str=replace(icode_str,"[/mycode]","</pre>",1,1,1)
end select
loop
icodestr2=icode_str end function
%>