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

怪我咯
Release: 2023-03-07 22:16:01
Original
2000 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 of php parse_str() function and parse_url() function when parsing URLs. methods and differences.

First look at php parse_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.

<?php
$url_name=&#39;http://www.php.cn/mytest.php?uid=5&pages=2&category=3&#39;;
$urlarr=parse_url($url_name);

//指定第二个参数
parse_str($urlarr[&#39;query&#39;],$parr);//$urlarr[&#39;query&#39;]的值为:uid=5&pages=2&category=3&#39;
print_r($parr);
echo "<br/><br/>";

//不指定第二个参数
parse_str($urlarr[&#39;query&#39;]);
echo $uid."<br/>";
echo $pages."<br/>";
echo $category."<br/>";

?>
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:

<?
$test = parse_url("http://www.php.cn/index.php?name=tank&sex=1#top");
print_r($test);
?>
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:

<?php
header("content-type:text/html;charset=utf-8");
$url ="http://www.php.cn/index.php?name=tank&sex=1#top";
echo "scheme:".parse_url($url,PHP_URL_SCHEME)."<br/>";
echo "host:".parse_url($url,PHP_URL_HOST)."<br/>";
echo "path:".parse_url($url,PHP_URL_PATH)."<br/>";
echo "query:".parse_url($url,PHP_URL_QUERY)."<br/>";
echo "fragment:".parse_url($url,PHP_URL_FRAGMENT)."<br/>";
?>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template