Created
April 17, 2020 16:04
-
-
Save jissereitsma/a8464997c7924457f44a89ca27730cdd to your computer and use it in GitHub Desktop.
Simple PHP script to destructure query arguments from an URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!isset($argv) || count($argv) !== 2) { | |
die('Usage: ' . basename(__FILE__ ). ' {URL}'."\n"); | |
} | |
$url = $argv[1]; | |
if (empty($url)) { | |
die("Empty URL"); | |
} | |
$queryString = parse_url($url, PHP_URL_QUERY); | |
$queryString = urldecode($queryString); | |
$queryArguments = explode('&', $queryString); | |
$arguments = []; | |
foreach($queryArguments as $queryArgument) { | |
$argument = explode('=', $queryArgument); | |
$value = $argument[1]; | |
if (preg_match('/^\{\"/', $value)) { | |
$value = json_decode($value, true); | |
} elseif (strstr($value, ',')) { | |
$value = explode(', ', $value); | |
} | |
$arguments[$argument[0]] = $value; | |
} | |
print_r($arguments); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment