parse_str
(PHP 4, PHP 5, PHP 7)
parse_str — Parses the string into variables
Description
$encoded_string
[, array &$result
] ) : void
Parses encoded_string
as if it were the query string
passed via a URL and sets variables in the current scope (or in the array
if result
is provided).
Parameters
-
encoded_string
-
The input string.
-
result
-
If the second parameter
result
is present, variables are stored in this variable as array elements instead.WarningUsing this function without the
result
parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.Dynamically setting variables in function's scope suffers from exactly same problems as register_globals.
Read section on security of Using Register Globals explaining why it is dangerous.
Changelog
Version | Description |
---|---|
7.2.0 |
Usage of parse_str() without a second parameter
now emits an E_DEPRECATED notice.
|
Examples
Example #1 Using parse_str()
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
// Recommended
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
// DISCOURAGED
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
?>
Because variables in PHP can't have dots and spaces in their names,
those are converted to underscores. Same applies to naming of
respective key names in case of using this function with
result
parameter.
Example #2 parse_str() name mangling
<?php
parse_str("My Value=Something");
echo $My_Value; // Something
parse_str("My Value=Something", $output);
echo $output['My_Value']; // Something
?>
Notes
Note:
All variables created (or values returned into array if second parameter is set) are already urldecode()d.
Note:
To get the current QUERY_STRING, you may use the variable $_SERVER['QUERY_STRING']. Also, you may want to read the section on variables from external sources.
Note:
The magic_quotes_gpc setting affects the output of this function, as parse_str() uses the same mechanism that PHP uses to populate the $_GET, $_POST, etc. variables.
See Also
- parse_url() - Parse a URL and return its components
- pathinfo() - Returns information about a file path
- http_build_query() - Generate URL-encoded query string
- urldecode() - Decodes URL-encoded string
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.parse-str.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.