Differences between the phpparse_str () function and the parse_url () function to parse URLs

Source: Internet
Author: User
In PHP, there are two URL-resolved functions: parse_str () and parse_url (). The parse_str function parses the query string to the variable. the parse_url function is used to parse the entire URL, and return the components. the previous article also explained the two functions. This article mainly introduces the usage and differences of the two functions when parsing URLs. In PHP, there are two URL-resolved functions: parse_str () and parse_url (). The parse_str function parses the query string to the variable. the parse_url function is used to parse the entire URL, and return the components, the previous

This article also explains the two functions separately.Php parse_str () and parse_url () functions are used to parse URLs.

First, let's look at phpParse_str function

The php parse_str function parses the query string to the variable.

The parse_str function has two parameters. The first parameter is the query string to be parsed and is required. The second parameter is used to set the variable that receives the parsed query string. The second parameter is optional. See the following example:

Here we use parse_url to obtain the URL query string. the parse_url function is described below.

 
"; // Do not specify the second parameter parse_str ($ urlarr ['query']); echo $ uid ."
"; Echo $ pages ."
"; Echo $ category ."
";?>

Code running result:

It can be seen that the method for setting the second parameter is different from obtaining the parsed value without setting the second parameter.

Set the second parameter. we can directly output the parsed query string in the form of an array.

The second parameter is not set. you need to use the parameter name of the query string as the variable name to obtain the value.

Php parse_url

The php parse_url 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.

See the following example:

 

Code running result:

We can also use this function as follows:

 ";echo "host:".parse_url($url,PHP_URL_HOST)."
";echo "path:".parse_url($url,PHP_URL_PATH)."
";echo "query:".parse_url($url,PHP_URL_QUERY)."
";echo "fragment:".parse_url($url,PHP_URL_FRAGMENT)."
";?>

Code running result:

[Recommended articles ]:

1. explain the definition and usage of the php parse_url () function

2. explain the php parse_str () function in the example.

The above is a detailed description of the difference between the php parse_str () function and the parse_url () function to parse the URL. For more information, see other related articles in the first PHP community!

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.