·您的位置: 首页 » 资源教程 » 编程开发 » ASP » UBB代码的实现(REPLACE方法)

UBB代码的实现(REPLACE方法)

类别: ASP教程  评论数:0 总得分:0
此段代码部分是从网上其他资料里获得,然后我加以修改及完善,用了比较笨拙的办法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) \'先对文本进行HTML编码
str=replace(str,chr(13)+chr(10),"<br>") \'将文本回车换行符换成HTML的换行
str=replace(str,chr(32),"&nbsp;") \'把文本空格换成HTML空格
tmpstr="icode" \'此变量将用于处理[MYCODE]xxxxxx[/MYCODE]中的文本xxxxxx
str=icodeStr2(str,"mycode") \'调用icodeStr2函数,将出现[mycode]xxx[/mycode]的地方换成<pre>xxx</pre>
str=icodeStr2(str,"url") \'将[url]xxx[/url]换成<a href=xxx target=_blank>xxx</a>
str=icodeStr2(str,"email") \'类似上面
if not unimage then str=icodeStr2(str,"image") \'若没有禁用贴图 对其进行替换 <img src=xxx>
str=icodeStr1(str,"url") \'将[url=yyy]xxx[/url]换成<a href=yyy target=_blank>xxx</a>
str=icodeStr1(str,"email") \'类似上面替换成EMAIL链接
str=icodeStr1(str,"color") \'替换成<font color=xxx>...</font>
if not unsound then str=icodeStr1(str,"sound")
\'若没有禁用贴歌,则替换成<embed ..>什么的
if not unsound then str=icodeStr1(str,"flash")
\'若没有禁用贴歌,则替换成<embed ..>什么的
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) \'把mycode中出现的[yyy]...[/yyy]原样显示出来,不明白哦,看后面吧
str=replace(str,tmpstr&"]","]",1,-1,1)
str=replace(str,"/"&tmpstr,"/",1,-1,1)
icode2Html=str \'把处理好的文本返回
End Function

\'idodeStr1函数处理的是UBB格式为[UBB标记=xxxxxx][/UBB标记]
function icodeStr1(icode_str,icodeKeyWord)
beginstr=1 \'记录开始的字符位置的变量
endstr=1 \'记录结束的字符位置的变量

\'下面对文本(字符串)中出现的icodeKeyWord 即ubb标记,进行查找替换,为方便解释我们以 文本内容="yyy[url=xxx][/url]zzz" 为例
do while icodeKeyWord="url" or icodeKeyWord="email" or icodeKeyWord="color" or icodeKeyWord="sound" or icodeKeyWord="flash"
beginstr=instr(beginstr,icode_str,"["&icodeKeyWord&"=",1)
\'找到文本中出现 "[url=" 的位置,本例值为4
if beginstr=0 then exit do \'如果为0说明没有这个标记则退出循环
endstr=instr(beginstr,icode_str,"]",1) \'如果有,则从4开始的地方找接下来的"]",它的位置记录在endstr中,本例中值该为12
if endstr=0 then exit do \'如果没有"]"则退出
laststr=instr(beginstr,icode_str,"[/"&icodeKeyWord&"]",1)
\'从刚刚4的位置开始,找文本中出现的匹配的一对即"[/url]",如果不存在则退出
if laststr=0 then exit do \'如果没有退出
LicodeKeyWord=icodeKeyWord \'把UBB标记用另一个变量保存下来
beginstr=beginstr+len(LicodeKeyWord)+2 \'开始的位置加上ubb标记的长度还要加2,其实是要得到xxx的开始位置
text=mid(icode_str,beginstr,endstr-beginstr) \'取出xxx放在text中
if icodeKeyWord="flash" then
\'对于flash只允许贴后缀名为.swf
swf=instr(1,text,".swf",1)
if swf=0 then exit do
end if
if icodeKeyWord="sound" then
\'对于sound允许贴下面几种格式
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) \'这个就开始做替换了,这里的text内容就是"xxx"
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&"\'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\'"&text&"\' target=\'_blank\'>&nbsp;下载&nbsp;</a>",1,1,1)
icode_str=replace(icode_str,"[/sound]","<br>",1,1,1)
case "flash"
\'对于.swf文件,除了用嵌入尺寸更大的插件来播放,还允许开大窗口来欣赏
icode_str=replace(icode_str,"[flash="&text&"]","<embed width=\'160\' height=\'160\' play=\'0\' loop=\'0\' src=\'"&text&"\'><br><br>&nbsp;&nbsp;&nbsp;画面点右键选择PLAY播放&nbsp;&nbsp;&nbsp;&nbsp;<a href=\'"&text&"\' target=\'_blank\'>开新窗口欣赏</a>",1,1,1)
icode_str=replace(icode_str,"[/flash]","<br>",1,1,1)

end select
loop \'循环找下一次出现的该ubb标记做处理
icodestr1=icode_str \'换好的文本返回
end function

\'idodeStr2函数处理的是UBB格式为[UBB标记]xxxxxx[/UBB标记]
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"
\'这段是处理[mycode]xxxxxx[/mycode],我们要考虑处理的是如果xxxxxx中出现[url]...[/url]这样的标记怎么办,为避免它们被替换,大家看到,在函数icode2Html中我们要先做mycode的处理,这点非常重要,我的思路是把xxxxxx中出现的"[url]...[/url]"先换成"[icodeurl]icode...[icode/icodeurl]icode",这样他们就不会被处理了,最后在函数icode2Html中,又把它们换回去
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 \'循环找下一次出现的该ubb标记做处理

icodestr2=icode_str \'换好的文本返回
end function
%>
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1