Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

parse_url

Description

The parse_url of URL for PHP parse a URL and return its components.

Syntax

parse_url(
    string $url,
    int $component = -1
): int|string|array|null|false

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 int).

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.

KeysDescription
scheme
host
port
user
pass
path
queryafter the question mark (?)
fragmentafter the hashtag (#)

If the component parameter is specified, parse_url() returns a string (or an int, 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. As of PHP 8.0.0, parse_url() distinguishes absent and empty queries and fragments:

http://example.com/foo → query = null, fragment = null
http://example.com/foo? → query = "",   fragment = null
http://example.com/foo# → query = null, fragment = ""
http://example.com/foo?# → query = "",   fragment = ""

Previously all cases resulted in query and fragment being null.

Examples

1 · 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
)

2 · component · PHP_URL_SCHEME

<?

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

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

echo $return;

?>
https

3 · component · PHP_URL_HOST

<?

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

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

echo $return;

?>
hostname

4 · component · PHP_URL_PORT

<?

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

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

echo $return;

?>
9090

5 · component · PHP_URL_USER

<?

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

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

echo $return;

?>
username

6 · component · PHP_URL_PASS

<?

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

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

echo $return;

?>
password

7 · component · PHP_URL_PATH

<?

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

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

echo $return;

?>
/path

8 · component · PHP_URL_QUERY

<?

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

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

echo $return;

?>
arg=value

9 · component · PHP_URL_FRAGMENT

<?

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

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

echo $return;

?>
anchor
HomeMenu