PHP – parse_url() function for multibyte character encodings

/**
 * parse_url() function for multibyte character encodings
 *
 * @param $url
 * @return mixed
 */
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;
}