PHP function: parse_str-PHP source code

Source: Internet
Author: User
Ec (2); usage: voidparse_str (string $ str [, array & amp; $ arr]) parse_str is used to parse (separate) the query string (QueryString) in the URL ), the query string refers to a URL? What follows, such as localhosttestresult. php? Nameanve & amp; age21, the query string is script ec (2); script

Usage: void parse_str (string $ str [, array & $ arr])

Parse_str is used to parse (separate) the Query String in a URL? What follows, such as http: // localhost/test/result. php? Name = anve & age = 21, the query string is "name = anve & age = 21 ".

Of course, in PHP, you can use the $ _ GET array to obtain the value of the query string, but sometimes parse_str is more convenient, especially when the variable name in the query string (corresponding to the name and age in the above example) is unknown.

Index.html:






Name:
Age:



Result. php:





$ String = $ _ SERVER [''query _ string'']; // use $ _ SERVER [''query _ string''] to obtain the query string.

Echo ''' $ string: ''. $ string .''
'';
Parse_str ($ string );
Echo ''$ name:''. $ name .''
'';
Echo ''$ age:''. $ age .''
''; // After parse_str, two variables $ name and $ age are generated and assigned correctly.

Parse_str ($ string, $ arr); // The second parameter $ arr is an array used to save the results, so that multiple variables are not generated as before.
Echo''
'';
Print_r ($ arr); // $ arr [''name''] and $ arr [''age'] are correctly assigned values.
Echo''
'';
?>

Note: parse_str will automatically perform urldecode (URL Decoding ). For example, enter "bobo" in the name of index.html. My test result is:



</

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.