UrlDecode 函數(ISO/Big5/UTF8)

由於 ASP 並未提供 UrlDecode 函數,必需自行撰寫. 列表三個不同語系(Big5/UTF8/ISO)下的 UrlDecode 函數

繁體中文語系下的 URLDecode 函數

Function URLDecode(enStr)
        if isNull(enStr) then
                URLDecode = ""
        else
                dim deStr
                dim c,i,v
                deStr=""
                for i=1 to len(enStr)
                        c=Mid(enStr,i,1)
                        if c="%" then
                                v=eval("&h"+Mid(enStr,i+1,2))
                                if v<128 br="br" then="then">                                        deStr=deStr&chr(v)
                                        i=i+2
                                else
                                        if isvalidhex(mid(enstr,i,3)) then
                                                if isvalidhex(mid(enstr,i+3,3)) then
                                                        v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
                                                        deStr=deStr&chr(v)
                                                        i=i+5
                                                else
                                                        v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
                                                        deStr=deStr&chr(v)
                                                        i=i+3
                                                end if
                                        else
                                                destr=destr&c
                                        end if
                                end if
                        else
                                if c="+" then
                                        deStr=deStr&" "
                                else
                                        deStr=deStr&c
                                end if
                        end if
                next
                URLDecode=deStr
        end if
end function

function isvalidhex(str)
        Dim c
        isvalidhex=true
        str=ucase(str)
        if len(str)<>3 then isvalidhex=false:exit function
        if left(str,1)<>"%" then isvalidhex=false:exit function
        c=mid(str,2,1)
        if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then
isvalidhex=false:exit function
        c=mid(str,3,1)
        if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then
isvalidhex=false:exit function
end function


英文語系下的 UrlDecode 函數

function URLDecode(enSstr)
        dim re, str
        set re = new RegExp

        str = Replace(enStr, "+", " ")

        re.Pattern = "%([0-9a-fA-F]{2})"
        re.Global = True
        URLDecode = re.Replace(str, GetRef("URLDecodeHex"))
end function

function URLDecodeHex(match, hex_digits, pos, source)
        URLDecodeHex = chr("&H" & hex_digits)
end function


UTF-8 語系下的 UrlDecode 函數

Function UrlDecode(enStr)
        dim c,i,i2,v,deStr,WeiS
        for i=1 to len(enStr)
                c=Mid(enStr,i,1)
                if c="%" then
                        v=c16to2(Mid(enStr,i+1,2))
                        WeiS=instr(v,"0")
                        v=right(v,len(v)-WeiS)
                        i=i+3
                        for i2=2 to WeiS-1
                                c=c16to2(Mid(enStr,i+1,2))
                                c=right(c,len(c)-2)
                                v=v & c
                                i=i+3
                        next
                        if len(c2to16(v)) =4 then
                                deStr=deStr & chrw(c2to10(v))
                        else
                                deStr=deStr & chr(c2to10(v))
                        end if
                        i=i-1
                else
                        if c="+" then
                                deStr=deStr&" "
                        else
                                deStr=deStr&c
                        end if
                end if
        next
        UrlDecode = deStr
end Function

Function c16to2(x)
        dim tempstr
        dim i:i=0

        for i=1 to len(trim(x))
                tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
                do while len(tempstr)<4 br="br">                        tempstr="0" & tempstr
                loop
                c16to2=c16to2 & tempstr
        next
end Function

Function c2to16(x)
        dim i:i=1
        for i=1 to len(x) step 4
                c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
        next
end Function

Function c2to10(x)
        c2to10=0
        if x="0" then exit function
        dim i:i=0
        for i= 0 to len(x) -1
                if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
        next
end Function

Function c10to2(x)
        dim sign, result
        result = ""
        sign = sgn(x)
        x = abs(x)
        if x = 0 then
                c10to2 = 0
                exit function
        end if
        do until x = "0"
                result = result & (x mod 2)
                x = x \ 2
        loop
        result = strReverse(result)
        if sign = -1 then
                c10to2 = "-" & result
        else
                c10to2 = result
        end if
end Function

Facebook

Previous
Next Post »