
Aug 9th, 2006, 13:41
|
 |
Moderator
|
|
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
URLDecode - The one - ASP/VBScript
The name of the function should say it all. It decodes anything encoded using Server.URLEncode(...).
I needed a complete function to use complement my new Ajax scripts ( Ajax requests are 'URL encoded' by the client). I found several scripts but each had a flaw/something missing.
I believe this is the one. Enjoy! 
PS.: If you like it, gimme some brownies!  (reputation)
- Code: Select all
Function UrlDecode(ByVal output)
On Error Resume Next
output = "" & output
If output<>"" Then
Dim i, h, ce, cd
' convert all pluses to spaces
output = Replace(output, "+", " ")
' single quotes, not latin accent character
output = Replace(output, "%22", Chr(34))
' convert %hexdigits to the character
i = InStr(1, output, "%")
Do While i>0
h = "&H" & Mid(output, i+1, 2) & ""
ce = "" & Mid(output, i, 3) & ""
cd = "" & Chr(CLng(h)) & ""
If cd <> "" Then
' HIDE % (%25 = %)
If cd = "%" Then cd = "-!-"
' % is special - replacing it now would cause
' an endless loop. so let's just mark its location
' and replace it at the end of the routine
Else
' if we can't produce a character, put string back
' and hide % sign until the end of the routine
cd = Replace(ce, "%", "-!-")
End If
' replace character
output = Replace(output, ce, cd)
' find next hex character
i = InStr(1, output, "%")
Loop
' put % signs back
output = Replace(output, "-!-", "%")
End If
URLDecode = output
End Function
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Last edited by spinal007; Aug 9th, 2006 at 14:06.
|