在ASP開發(fā)中,如果表單提交的時(shí)候,表單頁是UFT8編碼,而接收頁確實(shí)GB2312編碼或者反之的情況,如果參數(shù)值是中文,就會出現(xiàn)獲取到的參數(shù)值是亂碼的問題。下面有一個(gè)很不錯(cuò)的函數(shù)來解決這問題:
ASP/VB Code復(fù)制內(nèi)容到剪貼板
- 'URL地址解碼,用于utf-8與GB2312轉(zhuǎn)中文字符通用
- 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))