Php url parsing function: parse_url explanation
Parse_url-parses the URL and returns its components
Description
Array parse_url (string $ url)
This function parses a URL and returns an associated array, which contains various components in the URL.
This function is not used to verify the validity of a given URL, but is used to separate it into the parts listed below. Incomplete URLs are also accepted, and parse_url () tries to parse them correctly as much as possible.
Parameters
Url
URL to be parsed
Return Value
For URLs that are seriously unqualified, parse_url () may return FALSE and issue E_WARNING. Otherwise, an associated array is returned, which consists of (at least one ):
Scheme-such as http
Host
Port
User
Pass
Path
Query-question mark? After
Fragment-after the hash symbol #
Example
Parse_url () ExampleCopy codeThe Code is as follows: <? Php
$ Url = 'HTTP: // username: password @ hostname/path? Arg = value # anchor ';
Print_r (parse_url ($ url ));
?>
The above routine will output:
Array
(
[Scheme] => http
[Host] => hostname
[User] => username
[Pass] => password
[Path] =>/path
[Query] => arg = value
[Fragment] => anchor
)
We often send a large string of URLs. If we want to parse the URLs, we can only continue to parse the tokens. However, in php, you must learn to use the functions written by the predecessors, so that the development speed will be faster. Of course, if you want to parse the Website, you have to go to the relevant instructions. What you want to introduce today is the parse_url function in php.
This function is mainly used to parse the website address. First, let's take a look at this function prototype. Its official description is as follows:
Mixed parse_url (string $ url [, int $ component =-1])
Php officially gave an example, And I expanded it as follows to facilitate the explanation.Copy codeThe Code is as follows: <? Php
$ Url = 'HTTP: // username: password @ hostname/path? Arg1 = value1 & arg2 = value2 # anchor ';
Print_r (parse_url ($ url ));
?>
First, let's take a look at the output results of this program.
Array (
[Scheme] => http
[Host] => hostname
[User] => username
[Pass] => password
[Path] =>/path
[Query] => arg1 = value1 & arg2 = value2
[Fragment] => anchor
)
Here, scheme refers to his route, while host is the site URL, user is the user name, pass is the password, path is the path, and query is the parameter, fragment is the anchor.
In the "$ component" section, the following parameters are available for transmission.
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 Array.
Similarly, for example, if PHP_URL_PATH and PHP_URL_QUERY are used, the following examples are provided.Copy codeThe Code is 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 ");
?>
The output result is as follows:
/Path
Arg1 = value1 & arg2 = value2