Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Do the same than parse_str without max_input_vars limitation
<?php
/**
* do the same than parse_str without max_input_vars limitation
* @param $string array string to parse
* @return array query parsed
**/
function my_parse_str($string) {
$result = array();
// find the pairs "name=value"
$pairs = explode('&', $string);
$toEvaluate = ''; // we will do a big eval() at the end not pretty but simplier
foreach($pairs as $pair) {
list($name, $value) = explode('=', $pair, 2);
$name = urldecode($name);
if (strpos($name, '[') !== false) { // name is an array
$name = preg_replace('|\[|', '][', $name, 1);
$name = str_replace(array('\'', '[', ']'), array('\\\'', '[\'', '\']'), $name);
$toEvaluate .= '$result[\''.$name.' = '.urldecode($value).'; '; // $result['na']['me'] = 'value';
}
else {
$name = str_replace('\'', '\\\'', $name);
$toEvaluate .= '$result[\''.$name.'\'] = '.urldecode($value).'; '; // $result['name'] = 'value';
}
}
eval($toEvaluate);
return $result;
}
$array = my_parse_str($query);
?>
@joshbmarshall

This comment has been minimized.

Copy link

commented Sep 10, 2013

This has issues with strings - see my fork for a fix

@rubo77

This comment has been minimized.

Copy link

commented Oct 3, 2013

I enhanced this on my fork: https://gist.github.com/rubo77/6821632

  • no need for eval(), I use the original parse_str() on each part of the query, so no need for josh's fix
  • the result created in the second parameter like the behaviour in parse_str(): http://php.net/manual/function.parse-str.php
  • fixed the behaviour on empty strings
@boryo

This comment has been minimized.

Copy link

commented Mar 22, 2016

Alternative without eval: https://github.com/boryo/php_parse_str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.