Created
February 27, 2015 10:15
-
-
Save Mulkave/65daabb82752f9b9a0dd to your computer and use it in GitHub Desktop.
UTF-8 aware parse_url() replacement
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
// Courtesy of http://php.net/manual/en/function.parse-url.php#114817 | |
/** | |
* UTF-8 aware parse_url() replacement. | |
* | |
* @return array | |
*/ | |
function mb_parse_url($url) | |
{ | |
$enc_url = preg_replace_callback( | |
'%[^:/@?&=#]+%usD', | |
function ($matches) | |
{ | |
return urlencode($matches[0]); | |
}, | |
$url | |
); | |
$parts = parse_url($enc_url); | |
if($parts === false) | |
{ | |
throw new \InvalidArgumentException('Malformed URL: ' . $url); | |
} | |
foreach($parts as $name => $value) | |
{ | |
$parts[$name] = urldecode($value); | |
} | |
return $parts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment