Multibyte-safe Parse URL function

@param string $url
@return array
@throws \InvalidArgumentException
486994
public static function multibyteParseUrl($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;
    }
273147
public static function normalize($url)
    {
        $u = parse_url(preg_replace('#^file:/+([a-z]+:)#i', '$1', $url));

        if ($u === false) {
            return $url;
        }

        if (empty($u['scheme'])) {
            $u = null;
            return false;
        }

        $scheme = strtolower($u['scheme']);

        if (strlen($scheme) > 1) {
            $normalized = $scheme . '://';
        } else {
            $normalized = strtoupper($scheme) . ':';
        }

        if (isset($u['user'])) {
            $normalized .= $u['user'];
            $normalized .= isset($u['pass']) ? (':' . $u['pass']) : '';
            $normalized .= '@';
        }

        if (isset($u['host'])) {
            if (in_array($scheme, static::$defaultSchemes)) {
                $host = urldecode($u['host']);
                $normalized .= mb_strtolower($host, mb_detect_encoding($host));
            } else {
                $normalized .= $u['host'];
            }
        }

        if (isset($u['port']) && !in_array($scheme . ':' . $u['port'], static::$defaultPorts)) {
            $normalized .= ':' . $u['port'];
        }

        if (empty($u['path']) || $u['path'] === '/') {
            $normalized .= '/';
        } else {
            $normalized .= '/' . ltrim(self::canonpath($u['path']), '/');
        }

        if (isset($u['query'])) {
            $normalized .= self::canonquery($u['query'], '?');
        }

        if (isset($u['fragment'])) {
            $normalized .= '#' . $u['fragment'];
        }

        $u = null;
        return $normalized;
    }
830977
public function extractShortUrl(string $url):array {
		$urls = [$url];

		while($url = $this->extract($url)){
			$urls[] = $url;
		}

		return $urls;
	}
1119767
public static function getPort(string $url)
    {
        $url = UrlHelper::validateUrl($url);

        $port = parse_url($url, PHP_URL_PORT);
        if ($port === false) {
            throw new InvalidArgumentException("Could not determine port, url seems to be invalid: $url");
        }

        return $port;
    }
883187
public static function convertUtfUrl($url)
    {
        $parts = (array) parse_url($url);

        if (isset($parts['host'])) {
            if (function_exists('idn_to_ascii')) {
                $parts['host'] = idn_to_ascii($parts['host']);
            }
        }

        if (isset($parts['path'])) {
            $parts['path'] = implode('/', array_map('urlencode', explode('/', $parts['path'])));
        }

        if (isset($parts['query'])) {
            parse_str($parts['query'], $query);

            $parts['query'] = http_build_query($query);
        }

        return self::buildUrl($parts);
    }
540478
protected function getUrlContents(string $url): string
    {
        $request = $this->getRequest($url);

        return $this->getParsedResponse($request);
    }
319092
private function getUriHostAndPath($url)
    {
        try {
            $uri = HttpUri::createFromString($url);
        } catch (InvalidArgumentException $e) {
            return ['', ''];
        }

        $host = $uri->getHost();
        $port = $uri->getPort();
        if (!$port && $uri->getScheme() == 'https') {
            $port = 443;
        }
        if ($port) {
            $host .= ':'.strval($port);
        }
        $path = $uri->getPath();
        if (!$path) {
            $path = '/';
        }

        return [$host, $path];
    }
953389
public static function _parse_url_query($url)
    {
        $array = parse_url($url);
        $query = $array['query'];

        $queryParts = explode('&', $query);

        $params = array();
        foreach ($queryParts as $param) {
            $item = explode('=', $param);
            $params[$item[0]] = empty($item[1]) ? '' : $item[1];
        }

        return $params;
    }
402078
protected function _parseFromString($uri)
    {
        if (false === $parts = parse_url($uri))
            throw new \InvalidArgumentException(sprintf(
                'Malformed uri (%s).'
                , $uri
            ));

        $this->scheme    = isset($parts['scheme'])   ? $parts['scheme']   : '';
        $this->userInfo  = isset($parts['user'])     ? $parts['user']     : '';
        $this->host      = isset($parts['host'])     ? $this->_filterHost($parts['host']) : '';
        $this->port      = isset($parts['port'])     ? $parts['port']     : null;
        $this->path      = isset($parts['path'])     ? $parts['path']     : '';
        $this->query     = isset($parts['query'])    ? $parts['query']    : '';
        $this->fragment  = isset($parts['fragment']) ? $parts['fragment'] : '';

        if (isset($parts['pass']))
            $this->userInfo .= ':' . $parts['pass'];
    }
1348129
private function parseUrl($url)
    {
        $parsed = parse_url($url);

        if ((count($parsed) === 1)
            && isset($parsed['path'])
            && (0 === strpos($parsed['path'], '?') || false !== strpos($parsed['path'], '&'))
        ) {
            $parsed = array(
                'query' => $parsed['path']
            );

            return $parsed;
        }

        return $parsed;
    }
1061603
public function parse($url)
    {
        $content = $this->performRequest($url)->getBody()->getContents();

        $content = $this->convertEncodingToUTF8($content);

        return $this->getHtmlDomParser($content);
    }
521995
public static function parse_URL($url)
	{
		$params = parse_url($url);
		$params['extras'] = array();
		if (isset($params['query']))
		{
			parse_str($params['query'], $params['extras']);
		}
		return $params;
	}
1031516
public static function parse_url( $url ) {
		$r             = wp_parse_url( $url );
		$r['pathname'] = empty( $r['path'] ) ? '/' : $r['path'];
		return $r;
	}
1317925
static function parse(string $url, ?int $preferredFormat = self::ABSOLUTE)
    {
        $components = parse_url($url);

        if ($components === false) {
            throw new InvalidUrlException(sprintf('The given URL "%s" is invalid', $url));
        }

        $query = [];

        if (isset($components['query'])) {
            parse_str($components['query'], $query);
        }

        return new static(
            $components['scheme'] ?? null,
            $components['host'] ?? null,
            $components['port'] ?? null,
            $components['path'] ?? '',
            $query,
            $components['fragment'] ?? null,
            $preferredFormat
        );
    }
72536
protected function parseUrlArray($url)
    {
        if (strpos($url['scheme'], '+')) {
            list($url['scheme'], $url['adapter']) = explode('+', $url['scheme']);
        }

        $defaultPort = $this->defaultPort();
        if ($defaultPort && empty($url['port'])) {
            $url['port'] = $defaultPort;
        }

        if (isset($url['query'])) {
            $extra = [];
            parse_str($url['query'], $extra);
            unset($url['query']);
            $url += $extra;
        }

        $url = array_merge($this->uriKeys, $url);

        foreach (['host', 'user', 'pass'] as $key) {
            if (!isset($url[$key])) {
                continue;
            }

            $url[$key] = urldecode($url[$key]);
        }

        foreach ($url as $key => $val) {
            $setter = 'set' . ucfirst($key);
            $this->$setter($val);
        }
    }
506974
protected function processURL(string $url) : array
    {
        $url = trim($url, '/');
        $url_array = explode('/', $url);
        return $url_array;
    }
100533
protected function _parseExtension($url)
    {
        if (count($this->_extensions) && strpos($url, '.') !== false) {
            foreach ($this->_extensions as $ext) {
                $len = strlen($ext) + 1;
                if (substr($url, -$len) === '.' . $ext) {
                    return [substr($url, 0, $len * -1), $ext];
                }
            }
        }

        return [$url, null];
    }