Definition and Usage
- PHP Version
- 4+
The parse_url() function parses the given URL string and returns URL components.
Returns URL Components such as scheme, host, port, user, pass, path, query, and fragment.
Basic Example
$url = 'https://www.example.com/path/to/resource?query=string&foo=bar';
var_dump(parse_url($url));
/*
array(4) {
["scheme"]=> string(5) "https"
["host"]=> string(15) "www.example.com"
["path"]=> string(17) "/path/to/resource"
["query"]=> string(20) "query=string&foo=bar"
}
*/
var_dump(parse_url($url, PHP_URL_SCHEME)); // string(5) "https"
var_dump(parse_url($url, PHP_URL_USER)); // NULL
var_dump(parse_url($url, PHP_URL_PASS)); // NULL
var_dump(parse_url($url, PHP_URL_HOST)); // string(15) "www.example.com"
var_dump(parse_url($url, PHP_URL_PORT)); // NULL
var_dump(parse_url($url, PHP_URL_PATH)); // string(17) "/path/to/resource"
var_dump(parse_url($url, PHP_URL_QUERY)); // string(20) "query=string&foo=bar"
var_dump(parse_url($url, PHP_URL_FRAGMENT)); // NULL
URL Components
To better understand URL Components, let's first look at the basic structure of a URL.
A URL can generally be divided into the following eight components.
Among these eight components, the scheme and host are required to form a typical web URL (absolute URL), while the remaining components can be optionally included in a URL depending on the specific situation or purpose.
scheme: Represents the protocol. Examples includehttp,https, andftp.user: Represents the username. Example:username(e.g.,ftp://username@host)pass: Represents the password used for authentication. Example:password(e.g.,ftp://username:password@host)host: Represents the domain name or IP address that identifies the server. Examples includewww.example.comand192.168.1.1.port: Represents the port number used to communicate with the server. It is specified when using a port other than the default port. For example, the default port for thehttpprotocol is80, and the default port forhttpsis443. Example:8080(e.g.,http://www.example.com:8080)path: Represents the path to a resource. Example:/path/to/resource(e.g.,http://www.example.com/path/to/resource)query: Represents the query string that contains additional information in a URL. It is located after?, and multiple parameters are separated by&. Example:key1=value1&key2=value2(e.g.,http://www.example.com/path?key1=value1&key2=value2)fragment: Indicates a specific section within a document or resource and starts with#. Example:section1(e.g.,http://www.example.com/path#section1)
Syntax
parse_url(string $url, int $component = -1): int|string|array|null|false
Parameters
$url |
Required. The URL string to parse. |
|---|---|
$component |
Optional. Used to specify the component to return. The default value is -1, which means that all components are returned as an associative array.
One of the following values can be used to return only a specific component.
|
Return Values
If a malformed URL is provided, the parse_url() function may return false.
Associative array or component value: The return value varies depending on the specified $component.
- When
$componentis not specified : The components included in the URL are returned as an associative array. The URL components become the keys of the associative array, and the values of the components become the values of those keys. Components that are not included in the URL do not exist in the returned associative array. Therefore, since the corresponding key does not exist, accessing a missing key may evaluate toNULLor trigger a warning. - When
$componentis specified : The value of the specified component is returned as a string, or as an integer whenPHP_URL_PORTis specified. If the component does not exist,NULLis returned.
The return types are summarized as follows.
| Situation | Return Type |
|---|---|
When $component is not specified |
array |
When one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY, or PHP_URL_FRAGMENT is specified |
string or NULL |
When PHP_URL_PORT is specified |
int or NULL |
| Malformed URL | false |
The returned values are not automatically percent-decoded, so they must be decoded separately if necessary.
Changelog
| Version | Description |
|---|---|
| Before 8.0.0 | The query and fragment elements returned by the parse_url() function were both set to NULL when the corresponding components were missing or empty in the URL. |
| From 8.0.0 | An empty string ("") is returned when the URL's query or fragment is empty, while NULL is returned when the component does not actually exist, allowing the two cases to be distinguished. |
http://example.com/foo → query = NULL, fragment = NULL http://example.com/foo? → query = NULL, fragment = NULL http://example.com/foo# → query = NULL, fragment = NULL http://example.com/foo?# → query = NULL, fragment = NULL
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 = ""
If a malformed URL is provided, the parse_url() function may return false.
$url = 'http://:80';
var_dump(parse_url($url)); // bool(false)
Notes
Although the parse_url() function can parse partial or partially invalid URLs, note that it is not designed to validate the given URL.
Additional Notes
- The
parse_url()function is designed to parse URLs, not general URIs. However, for backward compatibility, thefile://scheme that uses triple slashes (file:///...) is allowed as an exception. (This format is not valid for other schemes.) - In addition, the function is not designed to handle relative URLs, so it may not provide the expected results when a relative URL is provided.
URL Validation
The parse_url() function is used to analyze URL components and is not suitable for URL validation. If you want to validate a URL, PHP recommends using the filter_var() function with the FILTER_VALIDATE_URL filter.
You can easily validate a URL using the filter_var() function and the FILTER_VALIDATE_URL filter.
filter_var(string $url, FILTER_VALIDATE_URL)
/*
Returns true if $url is a valid URL; otherwise, returns false.
*/
To check whether a URL has a valid format, let's look at a simple example using the filter_var() function.
Note
The example code provided has limitations and cannot accurately validate all URLs. Additional appropriate measures may be required. In addition, when using it for security-related purposes, additional security validation measures may be necessary.
$user_input_url = $_POST['url'];
if (filter_var($user_input_url, FILTER_VALIDATE_URL)) {
echo 'The URL is valid.';
} else {
echo 'Invalid URL format.';
}
By doing this, you can easily validate a URL using the filter_var() function.
The parse_url() function should only be used for analyzing URL components.