wp_parse_url()WP 4.4.0

Parses a URL and returns its components. A wrapper for PHP's parse_url() function that handles consistency in the return values across PHP versions.

What exactly does the function do:

  • Standardizes (unifies) the work of parse_url() and makes it possible to specify:

    • URL without protocol (starts with //).
    • Relative URL (starts with /).

    Correct host definition for the unprotected link was fixed for parse_url() in PHP 5.4.7.

  • Suppresses errors returned by parse_url(). Before PHP 5.3.3 parse_url() generated an E_WARNING level error when it could not parse the URL.

Since 4.7.0 The $component parameter was added for parity with PHP's parse_url()

1 time — 0.000001 sec (speed of light) | 50000 times — 0.07 sec (speed of light) | PHP 7.2.5, WP 5.0

No Hooks.

Return

Mixed.

If the component is not specified:

  • array of URL components — on success.
  • false — on parse failure.

If the component is specified:

  • string/integer — the value of the component (an integer is returned for PHP_URL_PORT).
  • null — the component doesn't exist in the given URL.

See parse_url()'s return values.

Usage

wp_parse_url( $url, $component );
$url(string) (required)
The URL to parse.
$component(int)

The specific component to retrieve. Use one of the PHP predefined constants to specify which one:

PHP_URL_SCHEME
PHP_URL_HOST
PHP_URL_PORT
PHP_URL_USER
PHP_URL_PASS
PHP_URL_PATH
PHP_URL_QUERY
PHP_URL_FRAGMENT

Default: -1 (= return all parts as an array)

Examples

0

#1 Parse URL examples

$url = 'https://example.com/path?arg=value#anchor';

$parts = wp_parse_url( $url );

/*
$parts = Array (
	[scheme]   => https
	[host]     => example.com
	[path]     => /path
	[query]    => arg=value
	[fragment] => anchor
)
*/

Or get a single URL part:

echo wp_parse_url( $url, PHP_URL_SCHEME );   // https
echo wp_parse_url( $url, PHP_URL_HOST );     // example.com
echo wp_parse_url( $url, PHP_URL_PATH );     // /path
echo wp_parse_url( $url, PHP_URL_QUERY );    // arg=value
echo wp_parse_url( $url, PHP_URL_FRAGMENT ); // anchor

An example of parsing all possible URL components:

$url = 'http://username:password@hostname:9090/path?arg=value#anchor';

$parts = wp_parse_url( $url );
/*
$parts = Array (
	[scheme]   => http
	[host]     => hostname
	[port]     => 9090
	[user]     => username
	[pass]     => password
	[path]     => /path
	[query]    => arg=value
	[fragment] => anchor
)
*/

echo wp_parse_url( $url, PHP_URL_SCHEME );   // http
echo wp_parse_url( $url, PHP_URL_HOST );     // hostname
echo wp_parse_url( $url, PHP_URL_PORT );     // 9090
echo wp_parse_url( $url, PHP_URL_USER );     // username
echo wp_parse_url( $url, PHP_URL_PASS );     // password
echo wp_parse_url( $url, PHP_URL_PATH );     // /path
echo wp_parse_url( $url, PHP_URL_QUERY );    // arg=value
echo wp_parse_url( $url, PHP_URL_FRAGMENT ); // anchor
0

#2 Parsing URLs without protocol

$url = '//www.example.com/path?googleguy=googley';
$url_parts = wp_parse_url( $url );

/*
$url_parts = Array (
	[host] => www.example.com
	[path] => /path
	[query] => googleguy=googley
)
*/
0

#3 Parsing URLs without domain

$parts = wp_parse_url( '/no-dom?param=1#anchor' );

/*
Array (
	[path] => /no-dom
	[query] => param=1
	[fragment] => anchor
)
*/
0

#4 Fix a bug of port-like string

This function seems to fix a bug of parse_url(), where port-like string in URL is incorrectly parsed as a port:

$parts = wp_parse_url( '//example.com/foo:1234' );

/*
Array
(
	[host] => example.com
	[path] => /foo:1234
)
*/
$parts = parse_url( '//example.com/foo:1234' );

/*
Array
(
	[host] => example.com
	[port] => 1234
	[path] => /foo:1234
)
*/

Changelog

Since 4.4.0 Introduced.
Since 4.7.0 The $component parameter was added for parity with PHP's parse_url().

wp_parse_url() code WP 6.5.3

function wp_parse_url( $url, $component = -1 ) {
	$to_unset = array();
	$url      = (string) $url;

	if ( str_starts_with( $url, '//' ) ) {
		$to_unset[] = 'scheme';
		$url        = 'placeholder:' . $url;
	} elseif ( str_starts_with( $url, '/' ) ) {
		$to_unset[] = 'scheme';
		$to_unset[] = 'host';
		$url        = 'placeholder://placeholder' . $url;
	}

	$parts = parse_url( $url );

	if ( false === $parts ) {
		// Parsing failure.
		return $parts;
	}

	// Remove the placeholder values.
	foreach ( $to_unset as $key ) {
		unset( $parts[ $key ] );
	}

	return _get_component_from_parsed_url_array( $parts, $component );
}