ASP中文URL解码URLDecode函数实现

4.5k 记录 , 发表评论

大家都知道,在asp中,我们一般都通过 Server.UrlEncode 进行url的编码,这样使得一些特殊的字符能够通过链接正常访问,但当我们把这个编码后的url字符存入数据库后,有些时候需要程序读取这个url进行处理时,就需要对其进行url解码,在php中这些功能很完善,但asp中,我们是找不到Server.UrlDecode函数的,鉴于这个问题,我们就要自己写一个解码函数了,以下是一段支持中文URLDecode的Asp函数,可以基于这个函数将其写成一个类,呵呵,这里就不细说了。

<%
function URLDecode(strIn)
    URLDecode = ""
    Dim sl: sl = 1
    Dim tl: tl = 1
    Dim key: key = "%"
    Dim kl: kl = Len(key)

    sl = InStr(sl, strIn, key, 1)
    Do While sl>0
        If (tl=1 And sl<>1) Or tl<sl Then
            URLDecode = URLDecode & Mid(strIn, tl, sl-tl)
        End If

        Dim hh, hi, hl
        Dim a
        Select Case UCase(Mid(strIn, sl+kl, 1))
            Case "U":                  'Unicode URLEncode
            a = Mid(strIn, sl+kl+1, 4)
            URLDecode = URLDecode & ChrW("&H" & a)
            sl = sl + 6

            Case "E":                   'UTF-8 URLEncode
            hh = Mid(strIn, sl+kl, 2)
            a = Int("&H" & hh)          'ascii码
            If Abs(a)<128 Then
                sl = sl + 3
                URLDecode = URLDecode & Chr(a)
            Else
                hi = Mid(strIn, sl+3+kl, 2)
                hl = Mid(strIn, sl+6+kl, 2)
                a = ("&H" & hh And &H0F) * 2 ^12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
                If a<0 Then a = a + 65536
                URLDecode = URLDecode & ChrW(a)
                sl = sl + 9
            End If
        Case Else:                      'Asc URLEncode
            hh = Mid(strIn, sl+kl, 2)   '高位
            a = Int("&H" & hh)          'ascii码
            If Abs(a)<128 Then
            sl = sl + 3
            Else
            hi = Mid(strIn, sl+3+kl, 2) '低位
            a = Int("&H" & hh & hi)     '非ascii码
            sl = sl + 6
            End If
            URLDecode = URLDecode & Chr(a)
        End Select

        tl = sl
        sl = InStr(sl, strIn, key, 1)
    Loop

    URLDecode = URLDecode & Mid(strIn, tl)
End function
%>

转载自:http://v1.w4s.cn/support/url-decoding-support-chinese-asp-urldecode-function.htm

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

昵称 *