Skip to content

Instantly share code, notes, and snippets.

@joshbmarshall
Forked from Shagshag/gist:5849065
Last active December 22, 2015 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshbmarshall/6517321 to your computer and use it in GitHub Desktop.
Save joshbmarshall/6517321 to your computer and use it in GitHub Desktop.
Original gist would not work if a string had a character like : in it. Modified so that it would assign as a number if numeric otherwise assign as a string (escaping double quotes)
<?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);
$value = urldecode($value);
// If the value is a number, set as a number, otherwise escape double quotes and surround in double quotes
if (!is_numeric($value)) {
$value = '"' . str_replace('"', '\"', $value) . '"';
}
if (strpos($name, '[') !== false) { // name is an array
$name = preg_replace('|\[|', '][', $name, 1);
$name = str_replace(array('\'', '[', ']'), array('\\\'', '[\'', '\']'), $name);
$toEvaluate .= '$result[\'' . $name . ' = ' . $value . '; '; // $result['na']['me'] = 'value';
} else {
$name = str_replace('\'', '\\\'', $name);
$toEvaluate .= '$result[\'' . $name . '\'] = ' . $value . '; '; // $result['name'] = 'value';
}
}
eval($toEvaluate);
return $result;
}
$array = my_parse_str($query);
?>
@rubo77
Copy link

rubo77 commented Oct 3, 2013

This does not sent the correct result if an empty string is sent to the function, see my fork for a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment