Last active
October 11, 2020 01:40
-
-
Save claudiohilario/9a78042c2966bb7fa05609ba4a4d221f to your computer and use it in GitHub Desktop.
Function to extract query params of the uri
This file contains hidden or 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 | |
/** | |
* 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