Decode a URL (reverse of a URLEncode)

This function performs the opposite of the ASP Server.URLEncode function. Pass the function an encoded URL and a decoded URL is passed back.

Author: Veign
Catagory(s): ASP ,Miscellaneous ,
Language: VBScript
Type: Code Sample
Posted: December 27, 2005
Date Revised:
Notes: URL Decode
Limitations:

Code / Download

' ---------------------------------------------------------------
' Copyright ©2000 - 2014 Veign, LLC All rights reserved
' ---------------------------------------------------------------
' Distribution: You can freely use this code in your own
' applications, but you many not reproduce
' or publish this code on any web site,
' online service, or distribute as source on
' any media without express permission from Veign.
' ---------------------------------------------------------------

Private Function URLDecode(sURL)
    On Error Resume Next

    'Nothing passed
    If Len(sURL) = 0 Then
       URLDecode = ""
       Exit Function
    End If

    'Pluses become spaces
    Dim sOutput
    sOutput = Replace(sURL, "+", " ")

    'Convert %hexdigits to the character
    Dim aSplit
    aSplit = Split(sOutput, "%")

    Dim i
    If IsArray(aSplit) Then
      sOutput = aSplit(0)
      For I = 0 to UBound(aSplit) - 1
        sOutput = sOutput & _
                    Chr("&H" & Left(aSplit(i + 1), 2)) & _
                    Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
      Next
    End If

    'Pass back the decoded URL
    URLDecode = sOutput
End Function
Disclaimer: If you use any of the source code or compiled applications on this site, you realize that you do so AT YOUR OWN RISK. The items provided here are supplied AS IS and Veign makes no warranty as to its use or performance either expressed or implied.