Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Created April 17, 2017 15:31
Show Gist options
  • Save jesobreira/cfac2f4de6756e11b01fcb6aae886a09 to your computer and use it in GitHub Desktop.
Save jesobreira/cfac2f4de6756e11b01fcb6aae886a09 to your computer and use it in GitHub Desktop.
AutoIt ParseURL and ParseStr
#include <Array.au3> ; need only to do _ArrayDisplay, not needed by the lib
_ArrayDisplay(ParseStr("foo=bar&test=lol%20123"))
#cs
Result is:
[0][0] = 2
[0][1] = ununsed
[1][0] = foo
[1][1] = bar
[2][0] = test
[2][1] = lol 123
#ce
$aExample = ParseURL("https://google.com:8080/?name=doe")
MsgBox(0, "Test", "URL: " & $aExample[0] & @CRLF & _
"Protocol: " & $aExample[1] & @CRLF & _
"Domain: " & $aExample[2] & @CRLF & _
"Port: " & $aExample[3] & @CRLF & _
"Path: " & $aExample[4] & @CRLF & _
"Query string: " & $aExample[5])
Func ParseURL($sUrl)
Local $aMatches[6]
Local $sRegex = "^(?:([A-Za-z]+):)?\/{0,3}([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$"
Local $aMatches = StringRegExp($sUrl, $sRegex, 2) ; $STR_REGEXPARRAYFULLMATCH
Return $aMatches
EndFunc
Func ParseStr($sStr)
Local $aExplode = StringSplit($sStr, "&"),$aExplode2
Local $aResult[$aExplode[0]+1][2]
For $i = 1 To $aExplode[0]
$aExplode2 = StringSplit($aExplode[$i], "=")
$aResult[$i][0] = $aExplode2[1]
$aResult[$i][1] = $aExplode2[2]
; URL decode
$aResult[$i][1] = StringReplace($aResult[$i][1], "+", " ")
Local $matches = StringRegExp($aResult[$i][1], "\%([abcdefABCDEF0-9]{2})", 3)
If IsArray($matches) Then
For $match In $matches
$aResult[$i][1] = StringReplace($aResult[$i][1], "%" & $match, BinaryToString('0x' & $match))
Next
EndIf
Next
$aResult[0][0] = $aExplode[0]
Return $aResult
EndFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment