Jump to content

_urlencode() / _urldecode()


Recommended Posts

;===============================================================================
; _URLEncode()
; Description:  : Encodes a string to be URL-friendly
; Parameter(s):  : $toEncode       - The String to Encode
;                  : $encodeType = 0 - Practical Encoding (Encode only what is necessary)
;                  :             = 1 - Encode everything
;                  :             = 2 - RFC 1738 Encoding - http://www.ietf.org/rfc/rfc1738.txt
; Return Value(s): : The URL encoded string
; Author(s):  : nfwu
; Note(s):   : -
;
;===============================================================================
Func _URLEncode($toEncode, $encodeType = 0)
 Local $strHex = "", $iDec
 Local $aryChar = StringSplit($toEncode, "")
 If $encodeType = 1 Then;;Encode EVERYTHING
  For $i = 1 To $aryChar[0]
   $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
  Next
  Return $strHex
 ElseIf $encodeType = 0 Then;;Practical Encoding
  For $i = 1 To $aryChar[0]
   $iDec = Asc($aryChar[$i])
   if $iDec <= 32 Or $iDec = 37 Then
    $strHex = $strHex & "%" & Hex($iDec, 2)
   Else
    $strHex = $strHex & $aryChar[$i]
   EndIf
  Next
  Return $strHex
 ElseIf $encodeType = 2 Then;;RFC 1738 Encoding
  For $i = 1 To $aryChar[0]
   If Not StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", $aryChar[$i]) Then
    $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
   Else
    $strHex = $strHex & $aryChar[$i]
   EndIf
  Next
  Return $strHex
 EndIf
EndFunc
;===============================================================================
; _URLDecode()
; Description:  : Tranlates a URL-friendly string to a normal string
; Parameter(s):  : $toDecode - The URL-friendly string to decode
; Return Value(s): : The URL decoded string
; Author(s):  : nfwu
; Note(s):   : -
;
;===============================================================================
Func _URLDecode($toDecode)
 local $strChar = "", $iOne, $iTwo
 Local $aryHex = StringSplit($toDecode, "")
 For $i = 1 to $aryHex[0]
  If $aryHex[$i] = "%" Then
   $i = $i + 1
   $iOne = $aryHex[$i]
   $i = $i + 1
   $iTwo = $aryHex[$i]
   $strChar = $strChar & Chr(Dec($iOne & $iTwo))
  Else
   $strChar = $strChar & $aryHex[$i]
  EndIf
 Next
 Return StringReplace($strChar, "+", " ")
EndFunc

Example:

;;;;;;;;;;;;;;;;;;;;;
;;;;;;;EXAMPLE;;;;;;;
;;;;;;;;;;;;;;;;;;;;;
$testString = "This is a test. By nfwu. @CRLF here:"&@CRLF&"~!@#$%^&*()_+`-=\|';:""[]{},.<>/?"
MsgBox(0, "This is the string we'll use to test:", $testString)
$string0 = _URLEncode($testString, 0)
$string1 = _URLEncode($testString, 1)
$string2 = _URLEncode($testString, 2)
MsgBox(0, "ENCODED STRINGS", _
  "0(Practical): "&@CRLF&$string0&@CRLF&@CRLF& _
  "1(Evereything): "&@CRLF&$string1&@CRLF&@CRLF& _
  "2(RFC 1738): "&@CRLF&$string2 _
  )
$string0 = _URLDecode($string0)
$string1 = _URLDecode($string1)
$string2 = _URLDecode($string2)
MsgBox(0, "DECODED STRINGS", _
  "0(Practical): "&@CRLF&$string0&@CRLF&@CRLF& _
  "1(Evereything): "&@CRLF&$string1&@CRLF&@CRLF& _
  "2(RFC 1738): "&@CRLF&$string2 _
  )

What do you think?

#)

EDIT: thanks to the RTF poster, i had to edit it...

Edited by nfwu
Link to post
Share on other sites
  • 5 months later...

Would like to sneak in a little thank you into the multitude of replies to your post :)

Anyway, this is just what I was looking for. I will give it a try.

Thank you.

“Give a man a script; you have helped him for today. Teach a man to script; and you will not have to hear him whine for help.”AutoIt4UE - Custom AutoIt toolbar and wordfile for UltraEdit/UEStudio users.AutoIt Graphical Debugger - A graphical debugger for AutoIt.SimMetrics COM Wrapper - Calculate string similarity.

Link to post
Share on other sites
  • 8 months later...
  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...