parse_url

Parse a URL and return its components

Syntax

parse_url ( string $url [, int $component = -1 ] ) : mixed

Parameters

url

The URL to parse. Invalid characters are replaced by _.

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

On seriously malformed URLs, parse_url() may return FALSE. If the component parameter is omitted, an associative array is returned. At least one element will be present within the array. Potential keys within this array are:

Keys Description
scheme
host
port
user
pass
path
query after the question mark (?)
fragment after the hashtag (#)

If the component parameter is specified, parse_url() returns a string (or an integer, in the case of PHP_URL_PORT) instead of an array. If the requested component doesn't exist within the given URL, NULL will be returned.

Examples

url

<?

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

$return = parse_url($url);

print_r($return);

?>
Array
(
    [scheme] => https
    [host] => hostname
    [port] => 9090
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

component | PHP_URL_SCHEME

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
https

component | PHP_URL_HOST

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
hostname

component | PHP_URL_PORT

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
9090

component | PHP_URL_USER

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
username

component | PHP_URL_PASS

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
password

component | PHP_URL_PATH

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
/path

component | PHP_URL_QUERY

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
arg=value

component | PHP_URL_FRAGMENT

<?

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

$return = parse_url($url, $component);

print_r($return);

?>
anchor

PHP

Home Menu