Skip to content

Instantly share code, notes, and snippets.

@edderrd
Created March 14, 2012 20:57
Show Gist options
  • Save edderrd/2039467 to your computer and use it in GitHub Desktop.
Save edderrd/2039467 to your computer and use it in GitHub Desktop.
PHP: Parse url params
<?php
/**
* Parses a url to extract the query parameters from it as a assoc array
* @param string $url
* @param bool $decode (optional) apply url decode
* @return array
*/
function parseUrl($url, $decode = false)
{
$urlData = parse_url($url);
if (empty($urlData['query'])) { return null; }
$query = explode("&", $urlData['query']);
$parameters = array();
foreach($query as $parameter) {
$param = explode("=", $parameter);
if (!empty($param) && count($param) == 2)
$parameters[$param[0]] = $decode == true ? urldecode($param[1]) : $param[1];
}
return $parameters;
}
@dozer111
Copy link

hi, this is my solution:
i want to get www.siteName.com/controllerName/actionName?id=12&test1=test2

$url1=parse_url($url);
parse_str($url1['query'],$url1['query']);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment