Home >Backend Development >PHP Tutorial >Example of using the Parse_str function in PHP that can automatically split query characters_PHP tutorial
This article mainly introduces examples of using the Parse_str function in PHP that can automatically split query characters. The editor has not I have seen a function, now it is convenient to split the query string in the URL. Friends who need it can refer to it
Directly enter the code:
The code is as follows:
$str = "1&errid=1&fee=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 into variables.
Grammar
parse_str(string,array)
Parameter Description
string required. Specifies the string to be parsed.
array optional. Specifies the name of the array to store the variable. This parameter instructs the variable to be stored in an array.
Tips and Notes
Note: If the array parameter is not set, the variables set by this function will overwrite variables with the same name.
Note: The magic_quotes_gpc setting in php.ini affects the output of this function. If enabled, variables are converted by addslashes() before being parsed by parse_str() .
Example
Example 1
The code is as follows:
parse_str("id=23&name=John%20Adams");
echo $id."
";
echo $name;
?>
Output:
The code is as follows:
23
John Adams
Example 2
The code is as follows:
parse_str("id=23&name=John%20Adams",$myArray);
print_r($myArray);
?>
Output:
Copy the code. The code is as follows:
Array
(
[id] => 23
[name] => John Adams
)