Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

parse_url()

Split a URL without decoding its percent escapes.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Split a URL without decoding its percent escapes. Request a component to obtain its string or integer port, or null if absent. Since PHP 8 a present but empty query is the empty string. Malformed input can return false; successful parsing does not establish validity.

Signature

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

Example output

[8443,"/a%20b","",null]

Examples

<?php
$url = "https://example.test:8443/a%20b?";
echo json_encode([parse_url($url,PHP_URL_PORT),parse_url($url,PHP_URL_PATH),parse_url($url,PHP_URL_QUERY),parse_url($url,PHP_URL_FRAGMENT)],JSON_UNESCAPED_SLASHES);

Result

[8443,"/a%20b","",null]

Sources