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."
"; ?>
Code running results:
#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("//m.sbmmt.com/index.php?name=tank&sex=1#top"); print_r($test); ?>
Code running result:
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)."
"; ?>
Code running results:
[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!