PHP Parse URL function: Parse_url detailed
parse_url-resolves the URL and returns its components
Description
Array Parse_url (string $url)
This function resolves a URL and returns an associative array containing the various components that appear in the URL.
This function is not used to verify the legality of a given URL, but to decompose it into the sections listed below. An incomplete URL is also accepted, and Parse_url () tries to parse it as correctly as possible.
Parameters
Url
The URL to resolve
return value
The severely unqualified url,parse_url () may return FALSE and emit e_warning. Otherwise, an associative array is returned, and its components are (at least one):
scheme– such as HTTP
Host
Port
User
Pass
Path
query– at the question mark? After
fragment– after the hash symbol #
Example
Parse_url () example
Copy Code code as follows:
<?php
$url = ' Http://username:password@hostname/path?arg=value#anchor ';
Print_r (Parse_url ($url));
?>
The above routines will output:
Array
(
[Scheme] => http
[Host] => hostname
[User] => username
[Pass] => password
[Path] =>/path
[Query] => arg=value
[Fragment] => anchor
)
Often we will send a large list of URLs, and then to parse the words, can only continue to cut token to resolve. But in PHP, you must learn to use the previous written function, so that the development speed will be faster than others. Of course, to resolve the URL, you have to find the relevant instructions, and today to introduce, is the PHP parse_url this function.
This function is primarily used to parse URLs, and first look at the function prototype, which is officially described as follows:
Mixed Parse_url (string $url [, int $component =-1])
and the PHP official cited an example, and I will expand him as follows to facilitate the explanation.
Copy Code code as follows:
<?php
$url = ' Http://username:password@hostname/path?arg1=value1&arg2=value2#anchor ';
Print_r (Parse_url ($url));
?>
First, let's look at the output of this program.
Array (
[Scheme] => http
[Host] => hostname
[User] => username
[Pass] => password
[Path] =>/path
[Query] => arg1=value1&arg2=value2
[Fragment] => anchor
)
Among them, scheme refers to the route he took, and host is the platform URL, user is the user name, pass is the password, path is the route, query is the parameter, fragment is the anchor point.
In the "$component" part, the following parameters are available for delivery.
Php_url_scheme
Php_url_host
Php_url_user
Php_url_pass
Php_url_path
Php_url_query
Php_url_fragment
These parameters represent scheme, host, user, pass, path, query, and fragment in the array, respectively.
Again, for example, if you use Php_url_path and Php_url_query, here's a demonstration for him.
Copy Code code as follows:
<?php
$url = ' Http://username:password@hostname/path?arg1=value1&arg2=value2#anchor '; Echo nl2br (Parse_url ($url, Php_url_path). \ n ");
Echo nl2br (Parse_url ($url, Php_url_query). \ n ");
?>
And the output will be as follows:
/path
Arg1=value1&arg2=value2