首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> ASP教程
UBB代码的实现(replace方法)-ASP教程,脚本编码
作者:网友供稿 点击:49
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
此段代码部分是从网上其他资料里获得,然后我加以修改及完善,用了比较笨拙的办法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
%>


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·在ASP.NET中使用Office Web Components (OWC)创建统计图-.NET教程,数据库应用
·windows系统下jsp+mysql+tomcat的配置-JSP教程,Application/Applet
·把aspx文件编译成DLL文件-.NET教程,Asp.Net开发
·Haneng.com的简单留言板制作源程序例子-ASP教程,ASP应用
·使用VB实现邮箱自动注册(一):表单自动提交-.NET教程,VB.Net语言
·VS.NET安装指南(To菜鸟)-.NET教程,Asp.Net开发
·web下水晶报表的使用!-.NET教程,Web Service开发
·C# 静态成员和方法的学习小结-.NET教程,数据库应用
·IIS的使用-ASP教程,ASP基础
·asp讲座之二:读取通过表单发送的数据
最新文章
·photoshop鼠绘实例:浪漫夏夜壁纸_photoshop教程
·买卖中小网站交易的一些细节问题_站长心得
·七招打造最安全的windows xp操作系统_windows xp
·做google adsense最佳和最重要的要诀_网赚技巧
·上下文关联广告清单(内文广告)推荐_网赚技巧
·广告联盟,痛定思痛_网赚技巧
·insenz首批广告费发放给站长_网赚技巧
·李彦宏:三分之一时间用在寻找人才_站长访谈
·中国汽车资源网杨锁民:网络寒冬时下海_站长访谈
·做it新闻资讯网站应先学新浪_站长心得
相关主题
  • ubb代码转化html代码-ASP教程,脚本编码
  • UBB代码的实现(ASP)-ASP教程,ASP应用
  • UBB代码~(整理)-ASP教程,脚本编码
  • ubb代码的简单实现-ASP教程,ASP技巧
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭