Created
March 14, 2012 20:57
-
-
Save edderrd/2039467 to your computer and use it in GitHub Desktop.
PHP: Parse url params
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 | |
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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']);