ASP的URLDecode函数URLEncode解码函数
程序员文章站
2022-05-18 11:37:51
下面的代码是从kesion系统扒下的,确实不错,支持utf8格式。复制代码 代码如下:'==========================================...
下面的代码是从kesion系统扒下的,确实不错,支持utf8格式。
'================================================
'函数名:urldecode
'作 用:url解码
'================================================
function urldecode(byval urlcode)
dim start,final,length,char,i,butf8,pass
dim leftstr,rightstr,finalstr
dim b0,b1,bx,blength,position,u,utf8
on error resume next
b0 = array(192,224,240,248,252,254)
urlcode = replace(urlcode,"+"," ")
pass = 0
utf8 = -1
length = len(urlcode) : start = instr(urlcode,"%") : final = instrrev(urlcode,"%")
if start = 0 or length < 3 then urldecode = urlcode : exit function
leftstr = left(urlcode,start - 1) : rightstr = right(urlcode,length - 2 - final)
for i = start to final
char = mid(urlcode,i,1)
if char = "%" then
bx = urldecode_hex(mid(urlcode,i + 1,2))
if bx > 31 and bx < 128 then
i = i + 2
finalstr = finalstr & chrw(bx)
elseif bx > 127 then
i = i + 2
if utf8 < 0 then
butf8 = 1 : blength = -1 : b1 = bx
for position = 4 to 0 step -1
if b1 >= b0(position) and b1 < b0(position + 1) then
blength = position
exit for
end if
next
if blength > -1 then
for position = 0 to blength
b1 = urldecode_hex(mid(urlcode,i + position * 3 + 2,2))
if b1 < 128 or b1 > 191 then butf8 = 0 : exit for
next
else
butf8 = 0
end if
if butf8 = 1 and blength = 0 then butf8 = -2
if butf8 > -1 and utf8 = -2 then i = start - 1 : finalstr = "" : pass = 1
utf8 = butf8
end if
if pass = 0 then
if utf8 = 1 then
b1 = bx : u = 0 : blength = -1
for position = 4 to 0 step -1
if b1 >= b0(position) and b1 < b0(position + 1) then
blength = position
b1 = (b1 xor b0(position)) * 64 ^ (position + 1)
exit for
end if
next
if blength > -1 then
for position = 0 to blength
bx = urldecode_hex(mid(urlcode,i + 2,2)) : i = i + 3
if bx < 128 or bx > 191 then u = 0 : exit for
u = u + (bx and 63) * 64 ^ (blength - position)
next
if u > 0 then finalstr = finalstr & chrw(b1 + u)
end if
else
b1 = bx * &h100 : u = 0
bx = urldecode_hex(mid(urlcode,i + 2,2))
if bx > 0 then
u = b1 + bx
i = i + 3
else
if left(urlcode,1) = "%" then
u = b1 + asc(mid(urlcode,i + 3,1))
i = i + 2
else
u = b1 + asc(mid(urlcode,i + 1,1))
i = i + 1
end if
end if
finalstr = finalstr & chr(u)
end if
else
pass = 0
end if
end if
else
finalstr = finalstr & char
end if
next
urldecode = leftstr & finalstr & rightstr
end function
function urldecode_hex(byval h)
on error resume next
h = "&h" & trim(h) : urldecode_hex = -1
if len(h) <> 4 then exit function
if isnumeric(h) then urldecode_hex = cint(h)
end function
复制代码 代码如下:
'================================================
'函数名:urldecode
'作 用:url解码
'================================================
function urldecode(byval urlcode)
dim start,final,length,char,i,butf8,pass
dim leftstr,rightstr,finalstr
dim b0,b1,bx,blength,position,u,utf8
on error resume next
b0 = array(192,224,240,248,252,254)
urlcode = replace(urlcode,"+"," ")
pass = 0
utf8 = -1
length = len(urlcode) : start = instr(urlcode,"%") : final = instrrev(urlcode,"%")
if start = 0 or length < 3 then urldecode = urlcode : exit function
leftstr = left(urlcode,start - 1) : rightstr = right(urlcode,length - 2 - final)
for i = start to final
char = mid(urlcode,i,1)
if char = "%" then
bx = urldecode_hex(mid(urlcode,i + 1,2))
if bx > 31 and bx < 128 then
i = i + 2
finalstr = finalstr & chrw(bx)
elseif bx > 127 then
i = i + 2
if utf8 < 0 then
butf8 = 1 : blength = -1 : b1 = bx
for position = 4 to 0 step -1
if b1 >= b0(position) and b1 < b0(position + 1) then
blength = position
exit for
end if
next
if blength > -1 then
for position = 0 to blength
b1 = urldecode_hex(mid(urlcode,i + position * 3 + 2,2))
if b1 < 128 or b1 > 191 then butf8 = 0 : exit for
next
else
butf8 = 0
end if
if butf8 = 1 and blength = 0 then butf8 = -2
if butf8 > -1 and utf8 = -2 then i = start - 1 : finalstr = "" : pass = 1
utf8 = butf8
end if
if pass = 0 then
if utf8 = 1 then
b1 = bx : u = 0 : blength = -1
for position = 4 to 0 step -1
if b1 >= b0(position) and b1 < b0(position + 1) then
blength = position
b1 = (b1 xor b0(position)) * 64 ^ (position + 1)
exit for
end if
next
if blength > -1 then
for position = 0 to blength
bx = urldecode_hex(mid(urlcode,i + 2,2)) : i = i + 3
if bx < 128 or bx > 191 then u = 0 : exit for
u = u + (bx and 63) * 64 ^ (blength - position)
next
if u > 0 then finalstr = finalstr & chrw(b1 + u)
end if
else
b1 = bx * &h100 : u = 0
bx = urldecode_hex(mid(urlcode,i + 2,2))
if bx > 0 then
u = b1 + bx
i = i + 3
else
if left(urlcode,1) = "%" then
u = b1 + asc(mid(urlcode,i + 3,1))
i = i + 2
else
u = b1 + asc(mid(urlcode,i + 1,1))
i = i + 1
end if
end if
finalstr = finalstr & chr(u)
end if
else
pass = 0
end if
end if
else
finalstr = finalstr & char
end if
next
urldecode = leftstr & finalstr & rightstr
end function
function urldecode_hex(byval h)
on error resume next
h = "&h" & trim(h) : urldecode_hex = -1
if len(h) <> 4 then exit function
if isnumeric(h) then urldecode_hex = cint(h)
end function