当前位置:编程学习 > asp >>

UBB代码的实现(replace方法)

答案:此段代码部分是从网上其他资料里获得,然后我加以修改及完善,用了比较笨拙的办法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>。
用法:文本
[MYCODE][/MYCODE] 保留源码输入格式,替换为html标记<pre></pre>。其中的文本会完全保留,不会被UBB替换
[URL][/URL] 超连接,替换为html标记<a></a>。
用法:显示的文本(长格式)或连接地址(短格式),注意不要在“连接”的两端加引号。
[/EMAIL] email地址,替换为html标记<a href=>用法:人名[/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") '将xxx换成<a href=> str=icodeStr2(str,"email") '类似上面
if not unimage then str=icodeStr2(str,"image") '若没有禁用贴图 对其进行替换 <img src=xxx>
str=icodeStr1(str,"url") '将xxx换成<a href=> 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>",1,-1,1) '加粗
str=replace(str,"
","</b>",1,-1,1)
str=replace(str,"","<i>",1,-1,1) '倾斜
str=replace(str,"
","</i>",1,-1,1)
str=replace(str,"","<u>",1,-1,1) '加下划线
str=replace(str,"
","</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,"","<a href=> icode_str=replace(icode_str,"","</a>",1,1,1) '替换配对的一半
case "email" '如上
icode_str=replace(icode_str,"","<a href=> icode_str=replace(icode_str,"[/email]","</a>",1,1,1)
case "color"
icode_str=replace(icode_str,"","<font color='"&text&"'>",1,1,1)
icode_str=replace(icode_str,"
","</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=> icode_str=replace(icode_str,"[/sound]","<br>",1,1,1)
case "flash"
'对于.swf文件,除了用嵌入尺寸更大的插件来播放,还允许开大窗口来欣赏
icode_str=replace(icode_str,"[flash="&text&"]","<embed

上一个:以下代码如何修改,才能限制上传文件类型?
下一个:让使用者可以看到你的ASP的原代码

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,