ASP URL反编码函数代码
程序员文章站
2023-08-24 09:46:58
例如:我们在百度中搜索 萬仟网,则网址后面的参数就是http://www.baidu.com/s?cl=3&wd=%bd%c5%b1%be%d6%ae%bc%d2如果我们想...
例如:
我们在百度中搜索 ,则网址后面的参数就是
http://www.baidu.com/s?cl=3&wd=%bd%c5%b1%be%d6%ae%bc%d2
如果我们想将%bd%c5%b1%be%d6%ae%bc%d2还原为中文,使用下面的函数即可。
<%
response.write urldecode("http://www.baidu.com/s?cl=3&wd=%bd%c5%b1%be%d6%ae%bc%d2")
'================================================
'函数名: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
%>
我们在百度中搜索 ,则网址后面的参数就是
http://www.baidu.com/s?cl=3&wd=%bd%c5%b1%be%d6%ae%bc%d2
如果我们想将%bd%c5%b1%be%d6%ae%bc%d2还原为中文,使用下面的函数即可。
<%
response.write urldecode("http://www.baidu.com/s?cl=3&wd=%bd%c5%b1%be%d6%ae%bc%d2")
'================================================
'函数名: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
%>
下一篇: PHP中的extract的作用分析