Discuz教程网

asp中常用的字符串安全处理函数集合(过滤特殊字符等)

[复制链接]
authicon dly 发表于 2011-9-14 08:59:11 | 显示全部楼层 |阅读模式
  1. '=====================================
  2. '转换内容,防止意外
  3. '=====================================
  4. Function Content_Encode(ByVal t0)
  5. IF IsNull(t0) Or Len(t0)=0 Then
  6. Content_Encode=""
  7. Else
  8. Content_Encode=Replace(t0,"<","<")
  9. Content_Encode=Replace(Content_Encode,">",">")
  10. End IF
  11. End Function

  12. '=====================================
  13. '反转换内容
  14. '=====================================
  15. Function Content_Decode(ByVal t0)
  16. IF IsNull(t0) Or Len(t0)=0 Then
  17. Content_Decode=""
  18. Else
  19. Content_Decode=Replace(t0,"<","<")
  20. Content_Decode=Replace(Content_Decode,">",">")
  21. End IF
  22. End Function

  23. '=====================================
  24. '过滤字符
  25. '=====================================
  26. Function FilterText(ByVal t0,ByVal t1)
  27. IF Len(t0)=0 Or IsNull(t0) Or IsArray(t0) Then FilterText="":Exit Function
  28. t0=Trim(t0)
  29. Select Case t1
  30. Case "1"
  31. t0=Replace(t0,Chr(32)," ")
  32. t0=Replace(t0,Chr(13),"")
  33. t0=Replace(t0,Chr(10)&Chr(10),"<br>")
  34. t0=Replace(t0,Chr(10),"<br>")
  35. Case "2"
  36. t0=Replace(t0,Chr(8),"")'回格
  37. t0=Replace(t0,Chr(9),"")'tab(水平制表符)
  38. t0=Replace(t0,Chr(10),"")'换行
  39. t0=Replace(t0,Chr(11),"")'tab(垂直制表符)
  40. t0=Replace(t0,Chr(12),"")'换页
  41. t0=Replace(t0,Chr(13),"")'回车 chr(13)&chr(10) 回车和换行的组合
  42. t0=Replace(t0,Chr(22),"")
  43. t0=Replace(t0,Chr(32),"")'空格 SPACE
  44. t0=Replace(t0,Chr(33),"")'!
  45. t0=Replace(t0,Chr(34),"")'"
  46. t0=Replace(t0,Chr(35),"")'#
  47. t0=Replace(t0,Chr(36),"")'$
  48. t0=Replace(t0,Chr(37),"")'%
  49. t0=Replace(t0,Chr(38),"")'&
  50. t0=Replace(t0,Chr(39),"")''
  51. t0=Replace(t0,Chr(40),"")'(
  52. t0=Replace(t0,Chr(41),"")')
  53. t0=Replace(t0,Chr(42),"")'*
  54. t0=Replace(t0,Chr(43),"")'+
  55. t0=Replace(t0,Chr(44),"")',
  56. t0=Replace(t0,Chr(45),"")'-
  57. t0=Replace(t0,Chr(46),"")'.
  58. t0=Replace(t0,Chr(47),"")'/
  59. t0=Replace(t0,Chr(58),"")':
  60. t0=Replace(t0,Chr(59),"")';
  61. t0=Replace(t0,Chr(60),"")'<
  62. t0=Replace(t0,Chr(61),"")'=
  63. t0=Replace(t0,Chr(62),"")'>
  64. t0=Replace(t0,Chr(63),"")'?
  65. t0=Replace(t0,Chr(64),"")'@
  66. t0=Replace(t0,Chr(91),"")'\
  67. t0=Replace(t0,Chr(92),"")'\
  68. t0=Replace(t0,Chr(93),"")']
  69. t0=Replace(t0,Chr(94),"")'^
  70. t0=Replace(t0,Chr(95),"")'_
  71. t0=Replace(t0,Chr(96),"")'`
  72. t0=Replace(t0,Chr(123),"")'{
  73. t0=Replace(t0,Chr(124),"")'|
  74. t0=Replace(t0,Chr(125),"")'}
  75. t0=Replace(t0,Chr(126),"")'~
  76. Case Else
  77. t0=Replace(t0, "&", "&")
  78. t0=Replace(t0, "'", "'")
  79. t0=Replace(t0, """", """)
  80. t0=Replace(t0, "<", "<")
  81. t0=Replace(t0, ">", ">")
  82. End Select
  83. IF Instr(Lcase(t0),"expression")>0 Then
  84. t0=Replace(t0,"expression","e&shy;xpression", 1, -1, 0)
  85. End If
  86. FilterText=t0
  87. End Function

  88. '=====================================
  89. '过滤常见字符及Html
  90. '=====================================
  91. Function FilterHtml(ByVal t0)
  92. IF Len(t0)=0 Or IsNull(t0) Or IsArray(t0) Then FilterHtml="":Exit Function
  93. IF Len(Sdcms_Badhtml)>0 Then t0=ReplaceText(t0,"<(\/|)("&Sdcms_Badhtml&")", "<$1$2")
  94. IF Len(Sdcms_BadEvent)>0 Then t0=ReplaceText(t0,"<(.[^>]*)("&Sdcms_BadEvent&")", "<$1$2")
  95. t0=FilterText(t0,0)
  96. FilterHtml=t0
  97. End Function

  98. Function GotTopic(ByVal t0,ByVal t1)
  99. IF Len(t0)=0 Or IsNull(t0) Then
  100. GotTopic=""
  101. Exit Function
  102. End IF
  103. Dim l,t,c, i
  104. t0=Replace(Replace(Replace(Replace(t0," "," "),""",chr(34)),">",">"),"<","<")
  105. l=Len(t0)
  106. t=0
  107. For I=1 To l
  108. c=Abs(Asc(Mid(t0,i,1)))
  109. IF c>255 Then t=t+2 Else t=t+1
  110. IF t>=t1 Then
  111. gotTopic=Left(t0,I)&"…"
  112. Exit For
  113. Else
  114. GotTopic=t0
  115. End IF
  116. Next
  117. GotTopic=Replace(Replace(Replace(Replace(GotTopic," "," "),chr(34),"""),">",">"),"<","<")
  118. End Function

  119. Function UrlDecode(ByVal t0)
  120. Dim t1,t2,t3,i,t4,t5,t6
  121. t1=""
  122. t2=False
  123. t3=""
  124. For I=1 To Len(t0)
  125. t4=Mid(t0,I,1)
  126. IF t4="+" Then
  127. t1=t1&" "
  128. ElseIF t4="%" Then
  129. t5=Mid(t0,i+1,2)
  130. t6=Cint("&H" & t5)
  131. IF t2 Then
  132. t2=False
  133. t1=t1&Chr(Cint("&H"&t3&t5))
  134. Else
  135. IF Abs(t6)<=127 then
  136. t1=t1&Chr(t6)
  137. Else
  138. t2=True
  139. t3=t5
  140. End IF
  141. End IF
  142. I=I+2
  143. Else
  144. t1=t1&t4
  145. End IF
  146. Next
  147. UrlDecode=t1
  148. End Function

  149. Function CutStr(byVal t0,byVal t1)
  150. Dim l,t,c,i
  151. IF IsNull(t0) Then CutStr="":Exit Function
  152. l=Len(t0)
  153. t1=Int(t1)
  154. t=0
  155. For I=1 To l
  156. c=Asc(Mid(t0,I,1))
  157. IF c<0 Or c>255 Then t=t+2 Else t=t+1
  158. IF t>=t1 Then
  159. CutStr=Left(t0,I)&"..."
  160. Exit For
  161. Else
  162. CutStr=t0
  163. End IF
  164. Next
  165. End Function

  166. Function CloseHtml(ByVal t0)
  167. Dim t1,I,t2,t3,Regs,Matches,J,Match
  168. Set Regs=New RegExp
  169. Regs.IgnoreCase=True
  170. Regs.Global=True
  171. t1=Array("p","div","span","table","ul","font","b","u","i","h1","h2","h3","h4","h5","h6")
  172. For I=0 To UBound(t1)
  173. t2=0
  174. t3=0
  175. Regs.Pattern="\<"&t1(I)&"( [^\<\>]+|)\>"
  176. Set Matches=Regs.Execute(t0)
  177. For Each Match In Matches
  178. t2=t2+1
  179. Next
  180. Regs.Pattern="\</"&t1(I)&"\>"
  181. Set Matches=Regs.Execute(t0)
  182. For Each Match In Matches
  183. t3=t3+1
  184. Next
  185. For j=1 To t2-t3
  186. t0=t0+"</"&t1(I)&">"
  187. Next
  188. Next
  189. CloseHtml=t0
  190. End Function
复制代码




上一篇:PHP insert语法详解
下一篇:Asp中通过简单的例子理解下ByVal和ByRef的用法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 10:45

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表