- Server.URLEncode解码函数:通过URL传递汉字的编码后的解码函数
- 发布时间:2008-06-20 14:09:43 浏览数:12277 发布者:superadmin 设置字体【大 中 小】
URL编码是指为了将信息通过URL进行传输,所以必须将某些含有特殊意义的字符进行替换的一种编码方式,在asp中我们都知道有一个Server.URLEncode的函数可以完成这个功能。即:
如果有空格就用%20代替,如果有其它字符就用%ASCII代替,如果有汉字等四个字节的字符,就用两个%ASCII来代替。不过有时候我们也需要将经过这种编码的字符串进行解码,但asp并没有提供相关的函数,这给我们处理问题带来了一定的麻烦。其实我们只要知道了编码规则后,就可以用asp代码来实现我们自己的URlDecode函数了。
程序代码
function urldecode(encodestr)
Dim newstr,havechar,lastchar,i,char_c,next_1_c,next_1_Num
newstr=""
havechar=false
lastchar=""
for i=1 to len(encodestr)
char_c=mid(encodestr,i,1)
if char_c="+" then
newstr=newstr & " "
elseif char_c="%" then
next_1_c=mid(encodestr,i+1,2)
next_1_num=cint("&H" & next_1_c)
if havechar then
havechar=false
newstr=newstr & chr(cint("&H" & lastchar & next_1_c))
else
if abs(next_1_num)<=127 then
newstr=newstr & chr(next_1_num)
else
havechar=true
lastchar=next_1_c
end if
end if
i=i+2
else
newstr=newstr & char_c
end if
next
urldecode=newstr
end function
#2楼发布者:superadmin时间:2008-06-20 14:09:57
我是在开发中发现使用伪静态开发站内搜索引擎,get方式传递keywords的时候,汉字会经过url编码处理,比如在百度中搜索“百度”的时候得到的连接是:http://www.baidu.com/s?wd=%B0%D9%B6%C8&cl=3 其中%B0%D9%B6%C8就是这两个汉字的编码,而这样的文字是无法在数据库搜索出结果的,只有解码之后才可以,在这个时候就需要一个解码的函数,也就是上文中的函数。封装成函数后使用起来就很方便了:
Dim key
key=request.form("keywords")
key=urldecode(key)