ASP 动态包含文件的改进

ASP 本身不支持动态包含文件,现在的动态包含是通过 FSO 把被包含的文件合并到主文件里再运行。以下也有把形如 <!--#include file="filename.asp" --> 的普通包含文件方式称作“传统引用”,用函数实现的动态包含文件称作“动态引用”。常见的程序如下:
Function include(filename)
 Dim re,content,fso,f,aspStart,aspEnd
 
 set fso=CreateObject("Scripting.FileSystemObject")
 set f=fso.OpenTextFile(server.mappath(filename))
 content=f.ReadAll
 f.close
 set f=nothing
 set fso=nothing
 
 set re=new RegExp
 re.pattern="^s*="
 aspEnd=1
 aspStart=inStr(aspEnd,content,"<%")+2
 do while aspStart>aspEnd+1
  Response.write Mid(content,aspEnd,aspStart-aspEnd-2)
  aspEnd=inStr(aspStart,content,"%>")+2
  Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write "))


  aspStart=inStr(aspEnd,content,"<%")+2
 loop
 Response.write Mid(content,aspEnd)
 set re=nothing
End Function

数据挖掘论坛

使用范例:include("youinc.asp") 数据挖掘交友

以上范例引自 http://www.blueidea.com/tech/program/2003/101.asp 数据挖掘工具

但这处函数在处理补包含的文件中还有包含文件时就不灵了。我在以上函数的基础上改进出来如下函数,在被包含文件中还有普通的包含文件 <!--#include file="filename.asp" --> 也可正常运行。

数据挖掘交友

Function includeconvert(oRegExp, strFilename, strBlock)
 Dim incStart, incEnd, match, oMatches, str, code
 "用提取ASP代码的相同方式提取出include 部分的文件名,其余部分原样输出
 code = ""
 incEnd = 1
 incStart = InStr(incEnd,strBlock,"<!--#include ") + 13 "要找个目标字符串<!--#include 正好是13个字符,所以要+13
 Do While incStart>incEnd+12 "两个引用间距最小就是连续的--><--#,incStart是从<!--#include起数13个字符,所以要比前一个incEnd要至少多 13-1 得到的>incEnd+12的条件
  str = Mid(strBlock,incEnd,incStart-incEnd-13)
  str = Replace(str, """", """""") "把单个双引号换成两个双引号
  str = Replace(str, VbCr, "")
  str = Replace(str, VbLf, "")
  str = Replace(str, VbCrLf, "")
  code = code & VbCrLf & "Response.Write """ & str & """"

数据挖掘论坛


  incEnd=InStr(incStart,strBlock,"-->")+3
  oRegExp.pattern="(w+)=""([^""]+)""" "匹配 file="filename.ext" 或 virtual="virtualname.ext",捕捉类型及文件名两个子串
  Set oMatches = oRegExp.Execute(Mid(strBlock,incStart,incEnd-incStart-3))
  Set match = oMatches(0) "确定只有一组捕捉时,要得到这一组匹配的子串,可以这样做,省去用 For Each match In oMatches …… Next
  code = code & include(Mid(strFilename, 1, InStrRev(strFilename, "/")) & match.SubMatches(1)) "Mid(filename, 1, InStrRev(filename, "/")) 是在被引用的子文件名有路径时,把路径提取出来,加在子文件中传统引用的文件名前面,以找到正确的打开文件路径,因为动态引用时的文件路径是相对主文件而言的。要第二个匹配子串用SubMatches(1)
  incStart = InStr(incEnd,strBlock,"<!--#include ")+13
 Loop
 str = Mid(strBlock,incEnd)
 str = Replace(str, """", """""") "把单个双引号换成两个双引号 数据挖掘工具
 str = Replace(str, VbCr, "")
 str = Replace(str, VbLf, "")
 str = Replace(str, VbCrLf, "")
 code = code & VbCrLf & "Response.Write """ & str & """"
 includeconvert = code
End Function
Function include(filename)
 Dim re, content, fso, f, aspStart, aspEnd, code
 Set fso=CreateObject("scripting.FileSystemObject")
 Set f=fso.OpenTextFile(Server.MapPath(filename))
 content=f.ReadAll
 f.close
 Set f=nothing
 Set fso=nothing
数据挖掘研究院

 code = ""
 aspEnd=1
 aspStart=InStr(aspEnd,content,"<%")+2
 Set re=new RegExp
 Do While aspStart>aspEnd+1
  "传统引用<!--#inclde 肯定是在ASP代码段以外的,所以先转。
  code = code & includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2))
  aspEnd=InStr(aspStart,content,"%>")+2
  re.pattern="^s*=" "这段正则替换原来是把 <% = str % > 换回成标准的 <%Response.Write str % >
  code = code & VbCrLf & re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ") "ASP块前面再加回车换行,以避免连接块之间多个 Response.Write在同一行的错误
  aspStart=InStr(aspEnd,content,"<%")+2
 Loop
 code = code & includeconvert (re, filename, Mid(content,aspEnd))
 Set re=nothing
 include = code


End Function
数据挖掘交友

方便起见,以上函数最终返回的是整合了包含文件的整个 ASP 代码,使用时还要再用 Execute 执行之,即使用时需要:Execute(include("file.asp"))。 数据挖掘交友

以上函数对被包含文件与主文件同一路径时测试通过,未对被包含文件与主文件路径不同的情况做进一步容错,时间有限,欢迎有兴趣的朋友提出意见和改进。 

数据挖掘工具

[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:使用xml http为网站增加域名查询功能
下一篇:MD5不可逆加密算法的ASP实现实例
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • MDAC2.8下载!
  • 软件架构的十大错误
  • 没啥好东西了,贴一段我现在在用的产生随机
  • ASP+JS处理复杂表单的生成与验证
  • 无法加载DLL(OCI.DLL),如何解决?
  • 学习ASP有用的代码(很有用哦!)
  • 动态web开发语言项目 驳“ASP低能论”
  • 郁闷的System.Web.Mail
  • 利用ASP远程注册DLL的方法
  • 改进的ASP备份SQLServer数据库
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • 软件架构的十大错误
  • JavaScript基础教程第四课
  • 改进的ASP备份SQLServer数据库
  • MVC构架实现之ASP
  • .net入门一些常见的东西
  • VisualStudio.NET2003不能调试ASP.Net程序
  • X.U.S.T原创:自己搭建IIS找ASP程序漏洞
  • 郁闷的System.Web.Mail
  • ASP错误代码总结
  • 从XML中读取数据!(Asp.net学习一)
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静