Php uses parse_url () to parse the URL implementation code (parse_url details)

Source: Internet
Author: User
Parse_url is used to parse a URL and return an Associated array, which contains various components in the URL. 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 () example

The code is as follows:


$ 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.

The code is as follows:


$ 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.

The code is as follows:


$ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.