Scripts 学盟
标题:
ASP 接收参数并自动识别编码 (UTF/GBK)
[打印本页]
作者:
Alvin
时间:
2011-3-26 22:51:06
标题:
ASP 接收参数并自动识别编码 (UTF/GBK)
直接上代码
<%
Function GetParameter(ByVal name, ByRef dictionary)
Dim values(), matches, match, i, cSet : cSet = "GBK"
With New RegExp
.Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
.Global = True
.IgnoreCase = True
Set matches = .Execute(dictionary)
ReDim values(matches.Count-1)
For i=0 To matches.Count-1
values(i) = matches(i).Submatches.Item(0)
Next
GetParameter = Unescape(Replace(Join(values, ", "), "+", " "))
' .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])+$"
.Pattern = "^(?:[\x00-\x7f]|[\xe0-\xef][\x80-\xbf]{2})+$"
If .Test(GetParameter) Then cSet = "UTF-8"
End with
With Server.CreateObject("ADODB.Stream")
.Type = 2
.Charset = "iso-8859-1"
.Open
.WriteText GetParameter
.Position = 0
.Charset = cSet
GetParameter = .ReadText(-1)
.Close
End With
End Function
Function QueryString(ByVal name)
QueryString = GetParameter(name, Request.QueryString)
End Function
Function Form(ByVal name)
Form = GetParameter(name, Request.Form)
End Function
%>
<%=QueryString("gbkParam")%><br />
<%=QueryString("utfParam")%><br />
复制代码
作者:
momo
时间:
2011-3-26 23:48:16
{:3_59:}前来拜读!
作者:
Yisin
时间:
2011-3-27 23:45:25
我看不懂啊
{:3_47:}
作者:
exitdos
时间:
2011-3-28 11:25:18
这个我也测试了,放在uft-8的页面里,也是识别不了gb2312传递过来的中文编码啊。希坛主有空看看。
作者:
Alvin
时间:
2011-3-28 12:07:16
回复
4#
exitdos
我测试了呀,使用下面代码
并且访问
http://localhost/test.asp?wd1=%E4%B8%AD%E6%96%87&wd2=%D6%D0%CE%C4
wd1,wd2 都接收成功
如果还有问题,可以加我QQ 78423497
<%@ language="vbscript" codepage="65001"%>
<%
Response.CharSet = "UTF-8"
Response.CodePage = 65001
Function GetParameter(ByVal name, ByRef dictionary)
Dim values(), matches, match, i, cSet : cSet = "GBK"
With New RegExp
.Pattern = "(?:^|&)" & Server.URLEncode(name) & "=([^&]*)"
.Global = True
.IgnoreCase = True
Set matches = .Execute(dictionary)
ReDim values(matches.Count-1)
For i=0 To matches.Count-1
values(i) = matches(i).Submatches.Item(0)
Next
GetParameter = Unescape(Replace(Join(values, ", "), "+", " "))
.Pattern = "^(?:[\x00-\x7f]|[\xe0-\xef][\x80-\xbf]{2})+$"
If .Test(GetParameter) Then cSet = "UTF-8"
End with
With Server.CreateObject("ADODB.Stream")
.Type = 2
.Charset = "iso-8859-1"
.Open
.WriteText GetParameter
.Position = 0
.Charset = cSet
GetParameter = .ReadText(-1)
.Close
End With
End Function
Function QueryString(ByVal name)
QueryString = GetParameter(name, Request.QueryString)
End Function
Function Form(ByVal name)
Form = GetParameter(name, Request.Form)
End Function
Response.Write QueryString("wd1") & "<br/>"
Response.Write QueryString("wd2")
%>
复制代码
欢迎光临 Scripts 学盟 (http://www.iscripts.org/)
Powered by Discuz! X2