在ASP开发中,如果表单提交的时候,表单页是UFT8编码,而接收页确实GB2312编码或者反之的情况,如果参数值是中文,就会出现获取到的参数值是乱码的问题。下面有一个很不错的函数来解决这问题:
ASP/VB Code复制内容到剪贴板
- 'URL地址解码,用于utf-8与GB2312转中文字符通用
- Function DecodeURI(ByVal s)
- s = UnEscape(s)
- Dim reg, cs
- cs = "GBK"
- Set reg = New RegExp
- reg.Pattern = "^(?:[\x00-\x7f]|[\xfc-\xff][\x80-\xbf]{5}|[\xf8-\xfb][\x80-\xbf]{4}|[\xf0-\xf7][\x80-\xbf]{3}|[\xe0-\xef][\x80-\xbf]{2}|[\xc0-\xdf][\x80-\xbf])+$"
- If reg.Test(s) Then cs = "UTF-8"
- Set reg = Nothing
- Dim sm
- Set sm = CreateObject("ADODB.Stream")
- With sm
- .Type = 2
- .Mode = 3
- .Open
- .CharSet = "iso-8859-1"
- .WriteText s
- .Position = 0
- .CharSet = cs
- DecodeURI = .ReadText(-1)
- .Close
- End With
- Set sm = Nothing
- End Function
在接收页可以这样得到正确的中文字符:
username=DecodeURI(server.URLEncode(username))