public static function parse_url(string $url, int $component = -1)
    {
        // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080
        // thus we parse here, before url-encoding
        $componentsFromUrl = parse_url($url);
        if ($componentsFromUrl === false) {
            return false;
        }

        $encodedUrl = preg_replace_callback('%[^:@/?#&=\.]+%usD', function ($matches) {
            return urlencode($matches[0]);
        }, $url);
        $components = parse_url($encodedUrl);

        if ($components === false) {
            return false;
        }

        foreach ($components as &$currentComponent) {
            $currentComponent = urldecode($currentComponent);
        }

        // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080
        if (array_key_exists('host', $componentsFromUrl)) {
            $components['host'] = $componentsFromUrl['host'];
        }
        if (array_key_exists('port', $componentsFromUrl)) {
            $components['port'] = (integer)$componentsFromUrl['port'];
        } else {
            unset($components['port']);
        }

        switch ($component) {
            case -1:
                return $components;
            case PHP_URL_SCHEME:
                return $components['scheme'];
            case PHP_URL_HOST:
                return $components['host'];
            case PHP_URL_PORT:
                return $components['port'];
            case PHP_URL_USER:
                return $components['user'];
            case PHP_URL_PASS:
                return $components['pass'];
            case PHP_URL_PATH:
                return $components['path'];
            case PHP_URL_QUERY:
                return $components['query'];
            case PHP_URL_FRAGMENT:
                return $components['fragment'];
            default:
                throw new \InvalidArgumentException('Invalid component requested for URL parsing.', 1406280743);
        }
    }
public static function parse_url(string $url, int $component = -1)
    {
        // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080
        // thus we parse here, before url-encoding
        $componentsFromUrl = parse_url($url);
        if ($componentsFromUrl === false) {
            return false;
        }

        $encodedUrl = preg_replace_callback('%[^:@/?#&=\.]+%usD', function ($matches) {
            return urlencode($matches[0]);
        }, $url);
        $components = parse_url($encodedUrl);

        if ($components === false) {
            return false;
        }

        foreach ($components as &$currentComponent) {
            $currentComponent = urldecode($currentComponent);
        }

        // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080
        if (array_key_exists('host', $componentsFromUrl)) {
            $components['host'] = $componentsFromUrl['host'];
        }
        if (array_key_exists('port', $componentsFromUrl)) {
            $components['port'] = (integer)$componentsFromUrl['port'];
        } else {
            unset($components['port']);
        }

        switch ($component) {
            case -1:
                return $components;
            case PHP_URL_SCHEME:
                return $components['scheme'];
            case PHP_URL_HOST:
                return $components['host'];
            case PHP_URL_PORT:
                return $components['port'];
            case PHP_URL_USER:
                return $components['user'];
            case PHP_URL_PASS:
                return $components['pass'];
            case PHP_URL_PATH:
                return $components['path'];
            case PHP_URL_QUERY:
                return $components['query'];
            case PHP_URL_FRAGMENT:
                return $components['fragment'];
            default:
                throw new \InvalidArgumentException('Invalid component requested for URL parsing.', 1406280743);
        }
    }
public static function parse_url ( string $ url , int $ component = - 1 ) { // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080 // thus we parse here, before url-encoding $ componentsFromUrl = parse_url ( $ url ) ; if ( $ componentsFromUrl === false ) { return false ; } $ encodedUrl = preg_replace_callback ( '%[^:@/?#&=\.]+%usD' , function ( $ matches ) { return urlencode ( $ matches [ 0 ] ) ; } , $ url ) ; $ components = parse_url ( $ encodedUrl ) ; if ( $ components === false ) { return false ; } foreach ( $ components as & $ currentComponent ) { $ currentComponent = urldecode ( $ currentComponent ) ; } // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080 if ( array_key_exists ( 'host' , $ componentsFromUrl ) ) { $ components [ 'host' ] = $ componentsFromUrl [ 'host' ] ; } if ( array_key_exists ( 'port' , $ componentsFromUrl ) ) { $ components [ 'port' ] = ( integer ) $ componentsFromUrl [ 'port' ] ; } else { unset ( $ components [ 'port' ] ) ; } switch ( $ component ) { case - 1 : return $ components ; case PHP_URL_SCHEME : return $ components [ 'scheme' ] ; case PHP_URL_HOST : return $ components [ 'host' ] ; case PHP_URL_PORT : return $ components [ 'port' ] ; case PHP_URL_USER : return $ components [ 'user' ] ; case PHP_URL_PASS : return $ components [ 'pass' ] ; case PHP_URL_PATH : return $ components [ 'path' ] ; case PHP_URL_QUERY : return $ components [ 'query' ] ; case PHP_URL_FRAGMENT : return $ components [ 'fragment' ] ; default : throw new \ InvalidArgumentException ( 'Invalid component requested for URL parsing.' , 1406280743 ) ; } }
Parse a URL and return its components, UTF-8 safe

@param string $url The URL to parse. Invalid characters are replaced by _.
@param integer $component Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string (except when PHP_URL_PORT is given, in which case the return value will be an integer).
@return mixed
Parse a URL and return its components UTF - 8 safe