Page MenuHomePhabricator
Diviner libphutil Tech Docs PhutilQueryStringParser

final class PhutilQueryStringParser
libphutil Technical Documentation (Parsers)

Utilities for parsing HTTP query strings.

The builtin functions in PHP (notably, parse_str() and automatic parsing prior to request handling) are not suitable in the general case because they silently convert some characters in parameter names into underscores.

For example, if you call parse_str() with input like this:

x.y=z

...the output is this:

array(
  'x_y' => 'z',
);

...with the . replaced with an underscore, _. Other characters converted in this way include space and unmatched opening brackets.

Broadly, this is part of the terrible legacy of register_globals. Since we'd like to be able to parse all valid query strings without destroying any data, this class implements a less-encumbered parser.

Methods

public function parseQueryString($query_string)

Parses a query string into a dictionary, applying PHP rules for handling array nomenclature (like a[]=1) in parameter names.

For a more basic parse, see parseQueryStringToPairList().

Parameters
string$query_stringQuery string.
Return
map<string, wild>Parsed dictionary.

public function parseQueryStringToPairList($query_string)

Parses a query string into a basic list of pairs, without handling any array information in the keys. For example:

a[]=1&a[]=2

...will parse into:

array(
  array('a[]', '1'),
  array('a[]', '2'),
);

Use parseQueryString() to produce a more sophisticated parse which applies array rules and returns a dictionary.

Parameters
string$query_stringQuery string.
Return
list<pair<string, string>>List of parsed parameters.

private function parseQueryKeyToArr($key, $val, &$input_arr)

Treats the key as a flat query that potentially has square brackets. If there are square brackets we parse them into an array.

Example input: $key = "email[0]"; $val = "my@example.com";

Example output: array("email" => array(0 => "my@example.com"));

Parameters
string$key$key
string$val$val
array&$input_arr$input_arr
Return
wild