asp之自动闭合HTML/ubb标签函数 附简单注释
程序员文章站
2023-08-26 16:50:48
这最近在pj的function库里看到的这个函数,感觉思路差了点,不过相对比较完美,只是闭合标签时的顺序问题,呵呵 修改一下数组arrtags里的各元素内容,可以达到闭合任...
这最近在pj的function库里看到的这个函数,感觉思路差了点,不过相对比较完美,只是闭合标签时的顺序问题,呵呵
修改一下数组arrtags里的各元素内容,可以达到闭合任何标签的功能。
在此,我给增加了一些注释,方便大家一起学习学习
function closeubb(strcontent)
'*************************************
'自动闭合ubb
'*************************************
dim arrtags, i, openpos, closepos, re, strmatchs, j, match
set re = new regexp '申明re对象
re.ignorecase = true '设置是否区分字符大小写
re.global = true '设置全局可用性
arrtags = array("code", "quote", "list", "color", "align", "font", "size", "b", "i", "u", "html") '建立数组,存储相关需要检测是否闭合的标签
for i = 0 to ubound(arrtags) '循环对数组里的每一个元素进行检测
openpos = 0 '初始化当前标签开始标记的个数
closepos = 0 '初始化当前标签结束标记的个数
re.pattern = "\[" + arrtags(i) + "(=[^\[\]]+|)\]" '开始分别正则判断开始与结束标记的个数
set strmatchs = re.execute(strcontent)
for each match in strmatchs
openpos = openpos + 1
next
re.pattern = "\[/" + arrtags(i) + "\]"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
closepos = closepos + 1
next
for j = 1 to openpos - closepos '当开始与结束标记数量不一致时,闭合当前标签
strcontent = strcontent + "[/" + arrtags(i) + "]"
next
next
closeubb = strcontent
set re = nothing
end function
closehtml的注释同上
function closehtml(strcontent)
'*************************************
'自动闭合html
'*************************************
dim arrtags, i, openpos, closepos, re, strmatchs, j, match
set re = new regexp
re.ignorecase = true
re.global = true
arrtags = array("p", "div", "span", "table", "ul", "font", "b", "u", "i", "h1", "h2", "h3", "h4", "h5", "h6")
for i = 0 to ubound(arrtags)
openpos = 0
closepos = 0
re.pattern = "\<" + arrtags(i) + "( [^\<\>]+|)\>"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
openpos = openpos + 1
next
re.pattern = "\</" + arrtags(i) + "\>"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
closepos = closepos + 1
next
for j = 1 to openpos - closepos
strcontent = strcontent + "</" + arrtags(i) + ">"
next
next
closehtml = strcontent
set re = nothing
end function
修改一下数组arrtags里的各元素内容,可以达到闭合任何标签的功能。
在此,我给增加了一些注释,方便大家一起学习学习
复制代码 代码如下:
function closeubb(strcontent)
'*************************************
'自动闭合ubb
'*************************************
dim arrtags, i, openpos, closepos, re, strmatchs, j, match
set re = new regexp '申明re对象
re.ignorecase = true '设置是否区分字符大小写
re.global = true '设置全局可用性
arrtags = array("code", "quote", "list", "color", "align", "font", "size", "b", "i", "u", "html") '建立数组,存储相关需要检测是否闭合的标签
for i = 0 to ubound(arrtags) '循环对数组里的每一个元素进行检测
openpos = 0 '初始化当前标签开始标记的个数
closepos = 0 '初始化当前标签结束标记的个数
re.pattern = "\[" + arrtags(i) + "(=[^\[\]]+|)\]" '开始分别正则判断开始与结束标记的个数
set strmatchs = re.execute(strcontent)
for each match in strmatchs
openpos = openpos + 1
next
re.pattern = "\[/" + arrtags(i) + "\]"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
closepos = closepos + 1
next
for j = 1 to openpos - closepos '当开始与结束标记数量不一致时,闭合当前标签
strcontent = strcontent + "[/" + arrtags(i) + "]"
next
next
closeubb = strcontent
set re = nothing
end function
closehtml的注释同上
复制代码 代码如下:
function closehtml(strcontent)
'*************************************
'自动闭合html
'*************************************
dim arrtags, i, openpos, closepos, re, strmatchs, j, match
set re = new regexp
re.ignorecase = true
re.global = true
arrtags = array("p", "div", "span", "table", "ul", "font", "b", "u", "i", "h1", "h2", "h3", "h4", "h5", "h6")
for i = 0 to ubound(arrtags)
openpos = 0
closepos = 0
re.pattern = "\<" + arrtags(i) + "( [^\<\>]+|)\>"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
openpos = openpos + 1
next
re.pattern = "\</" + arrtags(i) + "\>"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
closepos = closepos + 1
next
for j = 1 to openpos - closepos
strcontent = strcontent + "</" + arrtags(i) + ">"
next
next
closehtml = strcontent
set re = nothing
end function