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

  • 2021-07-09 07:35:44
  • OfStack

Directly on the code:


 $str = "1&errid=1&fee=2&balance=2582&fails=&msgid=634541149212681528&msg= All were sent successfully. ";
    parse_str($str, $output);
    echo $output['msgid'];  // Output 634541149212681528

Definition and usage

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

Grammar

parse_str(string,array)

Parameter description
string required. Specifies the string to be parsed.
array is optional. Specifies the name of the array in which the variables are stored. This parameter indicates that the variable is stored in an array.

Tips and Notes

Note: If the array parameter is not set, the variable set by this function will override the variable 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 parse_str () is resolved.

Example

Example 1


<?php
parse_str("id=23&name=John%20Adams");
echo $id."<br />";
echo $name;
?>

Output:

23
John Adams

Example 2

<?php
parse_str("id=23&name=John%20Adams",$myArray);
print_r($myArray);
?>

Output:

Array
(
[id] => 23
[name] => John Adams
)


Related articles: