asp之自動閉合HTML/ubb標簽函數(shù) 附簡單注釋
更新時間:2010年04月22日 01:18:57 作者:
在用到編輯器時,就會碰到一點,那就是標簽的閉合問題,這個問題非常嚴峻,因為這可能會導致網(wǎng)頁顯示的整體樣式受到破壞。
這最近在PJ的function庫里看到的這個函數(shù),感覺思路差了點,不過相對比較完美,只是閉合標簽時的順序問題,呵呵
修改一下數(shù)組arrTags里的各元素內(nèi)容,可以達到閉合任何標簽的功能。
在此,我給增加了一些注釋,方便大家一起學習學習
Function closeUBB(strContent)
'*************************************
'自動閉合UBB
'*************************************
Dim arrTags, i, OpenPos, ClosePos, re, strMatchs, j, Match
Set re = New RegExp '申明re對象
re.IgnoreCase = True '設(shè)置是否區(qū)分字符大小寫
re.Global = True '設(shè)置全局可用性
arrTags = Array("code", "quote", "list", "color", "align", "font", "size", "b", "i", "u", "html") '建立數(shù)組,存儲相關(guān)需要檢測是否閉合的標簽
For i = 0 To UBound(arrTags) '循環(huán)對數(shù)組里的每一個元素進行檢測
OpenPos = 0 '初始化當前標簽開始標記的個數(shù)
ClosePos = 0 '初始化當前標簽結(jié)束標記的個數(shù)
re.Pattern = "\[" + arrTags(i) + "(=[^\[\]]+|)\]" '開始分別正則判斷開始與結(jié)束標記的個數(shù)
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 '當開始與結(jié)束標記數(shù)量不一致時,閉合當前標簽
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
修改一下數(shù)組arrTags里的各元素內(nèi)容,可以達到閉合任何標簽的功能。
在此,我給增加了一些注釋,方便大家一起學習學習
復(fù)制代碼 代碼如下:
Function closeUBB(strContent)
'*************************************
'自動閉合UBB
'*************************************
Dim arrTags, i, OpenPos, ClosePos, re, strMatchs, j, Match
Set re = New RegExp '申明re對象
re.IgnoreCase = True '設(shè)置是否區(qū)分字符大小寫
re.Global = True '設(shè)置全局可用性
arrTags = Array("code", "quote", "list", "color", "align", "font", "size", "b", "i", "u", "html") '建立數(shù)組,存儲相關(guān)需要檢測是否閉合的標簽
For i = 0 To UBound(arrTags) '循環(huán)對數(shù)組里的每一個元素進行檢測
OpenPos = 0 '初始化當前標簽開始標記的個數(shù)
ClosePos = 0 '初始化當前標簽結(jié)束標記的個數(shù)
re.Pattern = "\[" + arrTags(i) + "(=[^\[\]]+|)\]" '開始分別正則判斷開始與結(jié)束標記的個數(shù)
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 '當開始與結(jié)束標記數(shù)量不一致時,閉合當前標簽
strContent = strContent + "[/" + arrTags(i) + "]"
Next
Next
closeUBB = strContent
Set re = Nothing
End Function
closehtml的注釋同上
復(fù)制代碼 代碼如下:
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
相關(guān)文章
msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法
今天發(fā)現(xiàn)一個asp后臺使用了XMLHTTP組件的頁面無法無法生成靜態(tài)頁面了,運行時提示msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191,經(jīng)過搜索如下方法解決了問題2020-11-11