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

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

程序员文章站 2022-10-27 15:50:32
复制代码 代码如下:'===================================== '转换内容,防止意外 '=========================...
复制代码 代码如下:

'=====================================
'转换内容,防止意外
'=====================================
function content_encode(byval t0)
if isnull(t0) or len(t0)=0 then
content_encode=""
else
content_encode=replace(t0,"<","<")
content_encode=replace(content_encode,">",">")
end if
end function

'=====================================
'反转换内容
'=====================================
function content_decode(byval t0)
if isnull(t0) or len(t0)=0 then
content_decode=""
else
content_decode=replace(t0,"<","<")
content_decode=replace(content_decode,">",">")
end if
end function

'=====================================
'过滤字符
'=====================================
function filtertext(byval t0,byval t1)
if len(t0)=0 or isnull(t0) or isarray(t0) then filtertext="":exit function
t0=trim(t0)
select case t1
case "1"
t0=replace(t0,chr(32)," ")
t0=replace(t0,chr(13),"")
t0=replace(t0,chr(10)&chr(10),"<br>")
t0=replace(t0,chr(10),"<br>")
case "2"
t0=replace(t0,chr(8),"")'回格
t0=replace(t0,chr(9),"")'tab(水平制表符)
t0=replace(t0,chr(10),"")'换行
t0=replace(t0,chr(11),"")'tab(垂直制表符)
t0=replace(t0,chr(12),"")'换页
t0=replace(t0,chr(13),"")'回车 chr(13)&chr(10) 回车和换行的组合
t0=replace(t0,chr(22),"")
t0=replace(t0,chr(32),"")'空格 space
t0=replace(t0,chr(33),"")'!
t0=replace(t0,chr(34),"")'"
t0=replace(t0,chr(35),"")'#
t0=replace(t0,chr(36),"")'$
t0=replace(t0,chr(37),"")'%
t0=replace(t0,chr(38),"")'&
t0=replace(t0,chr(39),"")''
t0=replace(t0,chr(40),"")'(
t0=replace(t0,chr(41),"")')
t0=replace(t0,chr(42),"")'*
t0=replace(t0,chr(43),"")'+
t0=replace(t0,chr(44),"")',
t0=replace(t0,chr(45),"")'-
t0=replace(t0,chr(46),"")'.
t0=replace(t0,chr(47),"")'/
t0=replace(t0,chr(58),"")':
t0=replace(t0,chr(59),"")';
t0=replace(t0,chr(60),"")'<
t0=replace(t0,chr(61),"")'=
t0=replace(t0,chr(62),"")'>
t0=replace(t0,chr(63),"")'?
t0=replace(t0,chr(64),"")'@
t0=replace(t0,chr(91),"")'\
t0=replace(t0,chr(92),"")'\
t0=replace(t0,chr(93),"")']
t0=replace(t0,chr(94),"")'^
t0=replace(t0,chr(95),"")'_
t0=replace(t0,chr(96),"")'`
t0=replace(t0,chr(123),"")'{
t0=replace(t0,chr(124),"")'|
t0=replace(t0,chr(125),"")'}
t0=replace(t0,chr(126),"")'~
case else
t0=replace(t0, "&", "&")
t0=replace(t0, "'", "'")
t0=replace(t0, """", """)
t0=replace(t0, "<", "<")
t0=replace(t0, ">", ">")
end select
if instr(lcase(t0),"expression")>0 then
t0=replace(t0,"expression","e­xpression", 1, -1, 0)
end if
filtertext=t0
end function

'=====================================
'过滤常见字符及html
'=====================================
function filterhtml(byval t0)
if len(t0)=0 or isnull(t0) or isarray(t0) then filterhtml="":exit function
if len(sdcms_badhtml)>0 then t0=replacetext(t0,"<(\/|)("&sdcms_badhtml&")", "<$1$2")
if len(sdcms_badevent)>0 then t0=replacetext(t0,"<(.[^>]*)("&sdcms_badevent&")", "<$1$2")
t0=filtertext(t0,0)
filterhtml=t0
end function

function gottopic(byval t0,byval t1)
if len(t0)=0 or isnull(t0) then
gottopic=""
exit function
end if
dim l,t,c, i
t0=replace(replace(replace(replace(t0," "," "),""",chr(34)),">",">"),"<","<")
l=len(t0)
t=0
for i=1 to l
c=abs(asc(mid(t0,i,1)))
if c>255 then t=t+2 else t=t+1
if t>=t1 then
gottopic=left(t0,i)&"…"
exit for
else
gottopic=t0
end if
next
gottopic=replace(replace(replace(replace(gottopic," "," "),chr(34),"""),">",">"),"<","<")
end function

function urldecode(byval t0)
dim t1,t2,t3,i,t4,t5,t6
t1=""
t2=false
t3=""
for i=1 to len(t0)
t4=mid(t0,i,1)
if t4="+" then
t1=t1&" "
elseif t4="%" then
t5=mid(t0,i+1,2)
t6=cint("&h" & t5)
if t2 then
t2=false
t1=t1&chr(cint("&h"&t3&t5))
else
if abs(t6)<=127 then
t1=t1&chr(t6)
else
t2=true
t3=t5
end if
end if
i=i+2
else
t1=t1&t4
end if
next
urldecode=t1
end function

function cutstr(byval t0,byval t1)
dim l,t,c,i
if isnull(t0) then cutstr="":exit function
l=len(t0)
t1=int(t1)
t=0
for i=1 to l
c=asc(mid(t0,i,1))
if c<0 or c>255 then t=t+2 else t=t+1
if t>=t1 then
cutstr=left(t0,i)&"..."
exit for
else
cutstr=t0
end if
next
end function

function closehtml(byval t0)
dim t1,i,t2,t3,regs,matches,j,match
set regs=new regexp
regs.ignorecase=true
regs.global=true
t1=array("p","div","span","table","ul","font","b","u","i","h1","h2","h3","h4","h5","h6")
for i=0 to ubound(t1)
t2=0
t3=0
regs.pattern="\<"&t1(i)&"( [^\<\>]+|)\>"
set matches=regs.execute(t0)
for each match in matches
t2=t2+1
next
regs.pattern="\</"&t1(i)&"\>"
set matches=regs.execute(t0)
for each match in matches
t3=t3+1
next
for j=1 to t2-t3
t0=t0+"</"&t1(i)&">"
next
next
closehtml=t0
end function