Skip to content

Instantly share code, notes, and snippets.

@claudiohilario
Last active October 11, 2020 01:40
Show Gist options
  • Save claudiohilario/9a78042c2966bb7fa05609ba4a4d221f to your computer and use it in GitHub Desktop.
Save claudiohilario/9a78042c2966bb7fa05609ba4a4d221f to your computer and use it in GitHub Desktop.
Function to extract query params of the uri
<?php
/**
* Extract query params of the uri.
*
* @param $uri - The uri.
* E.g.: http://localhost:8080/users?limit=10&offset=10
*
* @return array - Returns an array with query params and values.
* E.g.: [
* 'limit' => '10',
* 'offset' => '10',
* ]
*/
function extractQueryParams($uri) {
$strQueryParams = parse_url($uri, PHP_URL_QUERY);
if(!$strQueryParams) {
return [];
}
$splitQueryParams = explode('&', $strQueryParams);
$queryParams = [];
foreach ($splitQueryParams as &$queryParam) {
$queryParam = explode('=', $queryParam);
$queryParams[$queryParam[0]] = $queryParam[1];
}
return $queryParams;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment