Example of using the Parse_str function that can automatically split query characters in PHP

Source: Internet
Author: User
This article mainly introduces the example of using the Parse_str function that can automatically split query characters in PHP. this is a function that has not been seen in the small Editor. it is convenient to split the query string in the URL, for more information, see Directly run the code:

The code is as follows:


$ Str = "1 & errid = 1 & balance = 2 & balance = 2582 & fails = & msgid = 634541149212681528 & msg = all sent successfully. ";
Parse_str ($ str, $ output );
Echo $ output ['msgid']; // output 634541149212681528

Definition and usage

The parse_str () function parses the query string to a variable.

Syntax

Parse_str (string, array)

Parameter description
String is required. Specifies the string to be parsed.
Array is optional. Specifies the name of the array that stores the variable. This parameter indicates that the variable is stored in the array.

Tips and comments

Note: If the array parameter is not set, the variables set by this function will overwrite the variables with the same name.

Note: The magic_quotes_gpc setting in php. ini affects the output of this function. If enabled, the variable will be converted by addslashes () before parse_str () resolution.

Example

Example 1

The code is as follows:


<? Php
Parse_str ("id = 23 & name = John % 20 Adams ");
Echo $ id ."
";
Echo $ name;
?>


Output:

The code is as follows:


23
John Adams


Example 2

The code is as follows:


<? Php
Parse_str ("id = 23 & name = John % 20 Adams", $ myArray );
Print_r ($ myArray );
?>


Output:

The code is as follows:


Array
(
[Id] => 23
[Name] => John Adams
)

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.