PHP 4, PHP 5, PHP 7, PHP 8
parse_url - Parse a URL and return its components

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

This function parses a URL and returns an associative array containing any of the various components of the URL that are present. The values of the array elements are not URL decoded.

This function is not meant to validate the given URL, it only breaks it up into the parts listed below. Partial and invalid URLs are also accepted, parse_url tries its best to parse them correctly.



The URL to parse.


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 Values

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:

scheme - e.g. http






query - after the question mark ?

fragment - after the hashmark #

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: → query = null, fragment = null → query = "",   fragment = null → query = null, fragment = "" → query = "",   fragment = ""

Previously all cases resulted in query and fragment being null.

Note that control characters (cf. ctype_cntrl) in the components are replaced with underscores (_).



This function may not give correct results for relative or invalid URLs, and the results may not even match common behavior of HTTP clients. If URLs from untrusted input need to be parsed, extra validation is required, e.g. by using filter_var with the FILTER_VALIDATE_URL filter.


This function is intended specifically for the purpose of parsing URLs and not URIs. However, to comply with PHP's backwards compatibility requirements it makes an exception for the file:// scheme where triple slashes (file:///...) are allowed. For any other scheme this is invalid.


Version Description
8.0.0 parse_url will now distinguish absent and empty queries and fragments.

Related Functions

Example of parse_url

Show all examples for parse_url

PHP Version:

Function parse_url:

URLs Functions

Most used PHP functions