Skip to content

Instantly share code, notes, and snippets.

@jissereitsma
Created April 17, 2020 16:04
Show Gist options
  • Save jissereitsma/a8464997c7924457f44a89ca27730cdd to your computer and use it in GitHub Desktop.
Save jissereitsma/a8464997c7924457f44a89ca27730cdd to your computer and use it in GitHub Desktop.
Simple PHP script to destructure query arguments from an URL
<?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