The difference between the php parse_str() function and the parse_url() function in parsing URLs

怪我咯
Release: 2023-03-07 22:16:01
Original
1920 people have browsed it

There are two functions for parsing URLs in PHP, namely the parse_str() function and the parse_url() function. The parse_str function parses the query string into a variable, and the parse_url function is used to parse the entire URL and return its components. Part, the previous

articles also explained these two functions respectively. This article mainly introduces the use ofphp parse_str() function and parse_url() function when parsing URLs. methods and differences.

First look at phpparse_str function

php parse_str function parses the query string into a variable .

The parse_str function has two parameters. The first parameter is the query string that needs 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. Chosen. Please see the following example:

Here we use parse_url to obtain the query string of the URL. The parse_url function is explained below.


"; //不指定第二个参数 parse_str($urlarr['query']); echo $uid."
"; echo $pages."
"; echo $category."
"; ?>
Copy after login

Code running results:

The difference between the php parse_str() function and the parse_url() function in parsing URLs

#It can be seen that the methods to obtain the parsed value are to set the second parameter or not to set the second parameter. different.

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

The second parameter is not set, and the parameter name of the query string needs to be used as the variable name to obtain the value.

php parse_url

php The parse_url function parses a URL and returns an associative array containing the various components that appear in the URL.

This function is not used to verify the validity of the given URL, but to break it down into the parts listed below. Incomplete URLs are also accepted and parse_url() will try to parse them as correctly as possible.

Please see the following example:

Copy after login

Code running result:

The difference between the php parse_str() function and the parse_url() function in parsing URLs

We can also use this function like this:

"; 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)."
"; ?>
Copy after login

Code running results:

The difference between the php parse_str() function and the parse_url() function in parsing URLs

[Related article recommendations]:

1.Detailed explanation of php parse_url() function Definition and usage

2.Explanation of php parse_str() function

The above is the detailed content of The difference between the php parse_str() function and the parse_url() function in parsing URLs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!