[PHP 4, PHP 5, PHP 7, PHP 8]
parse_url — Parse a URL and return its components
Description
parse_url[string $url
, int $component
= -1]:
int|string|array|null|false
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.
Parameters
url
The URL to parse.
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 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
- host
- port
- user
- pass
- path
- 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:
//example.com/foo → query = null, fragment = null //example.com/foo? → query = "", fragment = null //example.com/foo# → query = null, fragment = "" //example.com/foo?# → 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 [_
].
Changelog
8.0.0 | parse_url[] will now distinguish absent and empty queries and fragments. |
Examples
Example #1 A parse_url[] example
The above example will output:
array[8] { ["scheme"]=> string[4] "http" ["host"]=> string[8] "hostname" ["port"]=> int[9090] ["user"]=> string[8] "username" ["pass"]=> string[8] "password" ["path"]=> string[5] "/path" ["query"]=> string[9] "arg=value" ["fragment"]=> string[6] "anchor" } string[4] "http" string[8] "username" string[8] "password" string[8] "hostname" int[9090] string[5] "/path" string[9] "arg=value" string[6] "anchor"
Example #2 A parse_url[] example with missing scheme
The above example will output:
array[3] { ["host"]=> string[15] "www.example.com" ["path"]=> string[5] "/path" ["query"]=> string[17] "googleguy=googley" }
Notes
Caution
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.
Note:
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.
See Also
- pathinfo[] - Returns information about a file path
- parse_str[] - Parses the string into variables
- http_build_query[] - Generate URL-encoded query string
- dirname[] - Returns a parent directory's path
- basename[] - Returns trailing name component of path
- » RFC 3986
thomas at gielfeldt dot com ¶
10 years ago
[If you haven't yet] been able to find a simple conversion back to string from a parsed url, here's an example:
lauris [] lauris ! lv ¶
8 years ago
Here is utf-8 compatible parse_url[] replacement function based on "laszlo dot janszky at gmail dot com" work. Original incorrectly handled URLs with user:pass. Also made PHP 5.5 compatible [got rid of now deprecated regex /e modifier].
mys5droid at gmail dot com ¶
6 years ago
I have coded a function which converts relative URL to absolute URL for a project of mine. Considering I could not find it elsewhere, I figured I would post it here.
The following function takes in 2 parameters, the first parameter is the URL you want to convert from relative to absolute, and the second parameter is a sample of the absolute URL.
Currently it does not resolve '../' in the URL, only because I do not need it. Most webservers will resolve this for you. If you want it to resolve the '../' in the path, it just takes minor modifications.