欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp之自动闭合HTML/ubb标签函数附简单注释

程序员文章站 2022-06-05 12:18:40
function closeubb(strcontent) '************************************* '自动闭合ubb '*******...
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
openposopenpos = openpos + 1
next
re.pattern = "\[/" + arrtags(i) + "\]"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
closeposclosepos = closepos + 1
next
for j = 1 to openpos - closepos '当开始与结束标记数量不一致时,闭合当前标签
strcontentstrcontent = strcontent + "[/" + arrtags(i) + "]"
next
next
closeubb = strcontent
set re = nothing
end function

程序代码
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
openposopenpos = openpos + 1
next
re.pattern = "\</" + arrtags(i) + "\>"
set strmatchs = re.execute(strcontent)
for each match in strmatchs
closeposclosepos = closepos + 1
next
for j = 1 to openpos - closepos
strcontentstrcontent = strcontent + "</" + arrtags(i) + ">"
next
next
closehtml = strcontent
set re = nothing
end function
下面的是pjblog的函数代码,但没有注释,学习研究建议参考上面的注释
复制代码 代码如下:

'*************************************
'自动闭合ubb
'*************************************

function closeubb(strcontent)
dim arrtags, i, openpos, closepos, re, strmatchs, j, match
set re = new regexp
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
end function

'*************************************
'自动闭合html
'*************************************

function closehtml(strcontent)
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
end function