Don't use parse_str(), not unless you're very sure about what you're doing. (Explained below.) Instead, I would suggest something like JSON, which doesn't change unexpected variables, is fairly fast, standardized, and easier to generate/consume.
$str = '{"items":[1,2,3]}'
$obj = json_decode($data);
var_export($obj->items);
Yields:
array (
0 => 1,
1 => 2,
2 => 3,
)
"Why not parse_str?"
- Bad input can easily break your code by overwriting variables you don't expect
- Malicious input can introduce security risks.
For example, try this:
$input = '_SERVER[DOCUMENT_ROOT]=/foo';
parse_str($input);
echo($_SERVER['DOCUMENT_ROOT']);
Wow, someone managed to change one of the variables that was storing server-configuration data. This could easily break stuff like content-management code that will then save files to the wrong place, or to bypass "must be a subfolder of" checks.
parse_str(), is a potential security hole.parse_str('_SERVER[DOCUMENT_ROOT]=/tmp')which would be a pretty bad thing if later on you want to refer to$_SERVER['DOCUMENT_ROOT']for some other purpose like checking folder security.'a[]=1,2,3'come from?$a=array(1,2,3);or$a=array('1','2','3');