Wiki Home

Url Decode


Namespace: Wiki
Url Decode converts an URL Encoded string to the original string.

Using API call:
Function URLDecode

    ***
    *** If tcInput contains "%00", the string will be terminated at that
    *** character.
    ***

    *** URLDecodes a text string:
    * Replaces %hh tokens with ascii characters

    *** Input: tcInput - Text string to decode
    *** Return: Decoded string
    * Author: Albert Ballinger

    Lparameter tcInput

    tcInput = Chrtran(tcInput, "+", " ")

    *!*	UrlUnescape
    *!*	Converts escape sequences back into ordinary characters.

    *!*	HRESULT UrlUnescape(
    *!*	  LPTSTR pszURL,
    *!*	  LPTSTR pszUnescaped,
    *!*	  LPDWORD pcchUnescaped,
    *!*	  DWORD dwFlags
    *!*	);

    *!*	Parameters
    *!*	pszURL - [in/out] Pointer to a NULL-terminated string with
    *!*	    the URL. If dwFlags is set to URL_UNESCAPE_INPLACE, the
    *!*	    converted string is returned through this parameter.
    *!*	pszUnescaped - [out] Pointer to a buffer that will receive a
    *!*	    NULL-terminated string containing the unescaped version
    *!*	    of pszURL. If URL_UNESCAPE_INPLACE is set in dwFlags,
    *!*	    this parameter is ignored.
    *!*	pcchUnescaped - [in/out] Number of characters in the buffer
    *!*	    pointed to by pcchUnescaped. On entry, the value
    *!*	    pcchUnescaped points to is set to the size of the
    *!*	    buffer. If the function returns a success code, the
    *!*	    value that pcchUnescaped points to is set to the number
    *!*	    of characters written to that buffer, not counting the
    *!*	    terminating NULL character. If an E_POINTER error code
    *!*	    is returned, the buffer was too small, and the value
    *!*	    pcchUnescaped points to is set to the required number of
    *!*	    characters that the buffer must be able to contain. If
    *!*	    any other errors are returned, the value that
    *!*	    pcchUnescaped points to is undefined.
    *!*	dwFlags - [in] Flags that control which characters are
    *!*	    unescaped. It can be a combination of the following
    *!*	    flags. Flag Description
    *!*		URL_DONT_UNESCAPE_EXTRA_INFO Don't convert the #
    *!*	            or ? character, or any characters following them
    *!*	            in the string.
    *!*		URL_UNESCAPE_INPLACE Use pszURL to return the
    *!*	            converted string instead of pszUnescaped.

    *!*	Return Values
    *!*	Returns an OLE success code if successful. If the
    *!*	URL_UNESCAPE_INPLACE flag is not set, the value pointed to
    *!*	by pcchUnescaped will be set to the number of characters in
    *!*	the output buffer pointed to by pszUnescaped. Returns
    *!*	E_POINTER if the URL_UNESCAPE_INPLACE flag is not set and
    *!*	the output buffer is too small. The pcchUnescaped parameter
    *!*	will be set to the required buffer size. Otherwise, returns
    *!*	an OLE error value.

    *!*	Remarks
    *!*	An escape sequence has the form "%xy".

    *!*	Requirements
    *!*	  Version 5.00 and later of Shlwapi.dll

    *!*	  Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0
    *!*	      with Internet Explorer 5.0 or later).
    *!*	  Windows 95/98: Requires Windows 98 (or Windows 95 with
    *!*	      Internet Explorer 5.0 or later).
    *!*	  Header: Declared in shlwapi.h.
    *!*	  Import Library: shlwapi.lib.

    Declare Integer UrlUnescape In shlwapi.Dll As UrlUnescape ;
        string pszURL, ;
        string @ pszUnescaped, ;
        integer @ pcchUnescaped, ;
        integer dwFlags

    Local lcOutput, lnLength
    lnLength = Len(tcInput) + 1
    lcOutput = Replicate(Chr(0), lnLength)

    If 0 = UrlUnescape(tcInput, @lcOutput, @lnLength, 0)
        lcOutput = Left(lcOutput, lnLength)
        Return lcOutput
    Else
        Return ""
    Endif

Endfunc


Using Regular Expression:
Function URLDecode
    *** URLDecodes a text string:
    * Replaces %hh tokens with ascii characters

    *** Input: tcInput - Text string to decode
    *** Return: Decoded string
    * Author: Albert Ballinger

    Lparameter tcInput

    tcInput = Chrtran(tcInput, "+", " ")

    Local lnPcntPos, lcOutput, loMatches, loMatch
    lcOutput = ""

    loRegExp = Createobject("vbscript.regexp")

    With loRegExp
        .Pattern = "([^%]+)"
        .Global = .T.
        loMatches = .Execute("00" + tcInput)
    Endwith

    With loMatches
        If .Count > 0
            For Each loMatch In loMatches
                lcOutput = lcOutput + Chr(Evaluate("0x" + Left(loMatch.Value, 2))) + ;
                    SUBSTR(loMatch.Value, 3)
            Endfor
            Return Substr(lcOutput, 2)
        Else
            Return tcInput
        Endif
    Endwith

Endfunc


Using Alines():
Function URLDecode
    *** URLDecodes a text string:
    * Replaces %hh tokens with ascii characters
    * Assuming fewer than 65000 '%'s

    *** Input: tcInput - Text string to decode
    *** Return: Decoded string
    * Author: Albert Ballinger

    Lparameter tcInput

    Local lnPcntPos, lcOutput, lnPcntCount, i
    tcInput = Strtran(tcInput, "+", " ")

    Local Array laPercents[1]
    lnPcntCount = Alines(laPercents, Strtran(tcInput, "%", Chr(13)))

    lcOutput = laPercents[1]

    For i = 2 To lnPcntCount
        lcOutput = m.lcOutput + Chr(Evaluate('0x' + Left(laPercents[i], 2))) + ;
            substr(laPercents[i], 3)
    Endfor
    Return lcOutput
Endfunc

- Albert Ballinger
Category Web Development Category Code Samples
( Topic last updated: 2002.08.03 02:46:00 PM )