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 __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

public function current()
Inherited

This method is not documented.
Return
wild

public function key()
Inherited

This method is not documented.
Return
wild

public function next()
Inherited

This method is not documented.
Return
wild

public function rewind()
Inherited

This method is not documented.
Return
wild

public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

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