对动态包含文件方法的改进计划
ASP 本身不支持动态包含文件,现在的动态包含是通过 FSO 把被包含的文件合并到主文件里再运行。以下也有把形如 !--#include file=\"p\" -- 的普通包含文件方式称作“传统引用”,用函数实现的动态包含文件称作“动态引用”。常见的程序如下:
Function include(filename)
Dim re,content,fso,f,aspStart,aspEnd
set fso=CreateObject(\"leSystemObject\")
set f=enTextFile(ppath(filename))
content=adAll
ose
set f=nothing
set fso=nothing
set re=new RegExp
ttern=\"^\\s*=\"
aspEnd=1
aspStart=inStr(aspEnd,content,\"%\")+2
do while aspStartaspEnd+1
ite Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content,\"%\\\")+2
Execute(place(Mid(content,aspStart,aspEnd-aspStart-2),\"ite \"))
aspStart=inStr(aspEnd,content,\"%\")+2
loop
ite Mid(content,aspEnd)
set re=nothing
End Function
使用范例:include(\"p\")
以上范例引自
但这处函数在处理补包含的文件中还有包含文件时就不灵了。我在以上函数的基础上改进出来如下函数,在被包含文件中还有普通的包含文件 !--#include file=\"p\" -- 也可正常运行。
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 incStartincEnd+12 \'两个引用间距最小就是连续的----#,incStart是从!--#include起数13个字符,所以要比前一个incEnd要至少多 得到的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 \"ite \"\"\" str \"\"\"\"
incEnd=InStr(incStart,strBlock,\"--\")+3
ttern=\"(\\w+)=\"\"([^\"\"]+)\"\"\" \'匹配 file=\"t\" 或 virtual=\"t\",捕捉类型及文件名两个子串
Set oMatches = ecute(Mid(strBlock,incStart,incEnd-incStart-3))
Set match = oMatches(0) \'确定只有一组捕捉时,要得到这一组匹配的子串,可以这样做,省去用 For Each match In oMatches …… Next
code = code include(Mid(strFilename, 1, InStrRev(strFilename, \"/\")) bMatches(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 \"ite \"\"\" str \"\"\"\"
includeconvert = code
End Function
Function include(filename)
Dim re, content, fso, f, aspStart, aspEnd, code
Set fso=CreateObject(\"leSystemObject\")
Set f=enTextFile(pPath(filename))
content=adAll
ose
Set f=nothing
Set fso=nothing
code = \"\"
aspEnd=1
aspStart=InStr(aspEnd,content,\"%\")+2
孩子叔叔家运营一家不锈钢加工厂 Set re=new RegExp
Do While aspStartaspEnd+1
\'传统引用!--#inclde 肯定是在ASP代码段以外的,所以先转。
code = code includeconvert (re, filename, Mid(content,aspEnd,aspStart-aspEnd-2))
aspEnd=InStr(aspStart,content,\"%\\\")+2
ttern=\"^\\s*=\" \'这段正则替换原来是把 % = str % 换回成标准的 %ite str %
code = code VbCrLf place(Mid(content,aspStart,aspEnd-aspStart-2),\"ite \") \'ASP块前面再加回车换行,以避免连接块之间多个 ite在同一行的错误
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(\"p\"))。
以上函数对被包含文件与主文件同一路径时测试通过,未对被包含文件与主文件路径不同的情况做进一步容错,时间有限,欢迎有兴趣的朋友提出意见和改进。查看本文来源
兰州治疗男科医院哪家好新标家居定制怎么样
合肥治疗妇科习惯性流产费用
- 上一篇:寒流来袭慧智护眼尽显人文关怀图物业
- 下一篇:对脚本源代码进行加密覆盖
-
清洁狗狗耳垢小妙招位置
汤羹2022年06月13日
-
使用耳粉拔狗狗耳毛减少疼痛位置
汤羹2022年06月13日
-
使用哪些方法可以让泰迪犬学会握手位置
汤羹2022年06月13日
-
你的哈士奇会坐吗位置
汤羹2022年06月13日
-
你正在犯哪些秋季护理沙皮狗的错误位置
汤羹2022年06月13日
-
你真的适合饲养藏獒吗位置
汤羹2022年06月13日