--- layout: default title: The Query component redirect_from: - /5.0/components/parsers/ --- Parsers =======

Since version 1.5.0

The library provides the following classes to ease components parsing: - `QueryParser` to parse and deserialize a query string. - `QueryBuilder` to build a query string from a collection of key/value pairs.

The parsers and their functions alias are defined in the League\Uri namespace.

## QueryParser::extract ~~~php 'baz']; $parser = new QueryParser(); $arr =$parser->extract($query_string); // $arr = ['foo.bar' => 'bar', 'foo_bar' => baz']; ~~~

Since version 1.2.0 The alias function Uri\extract_query is available

~~~php 'baz']; $arr = Uri\extract_query($query_string); // $arr = ['foo.bar' => 'bar', 'foo_bar' => baz']; ~~~ ## QueryParser::parse ~~~php parse($query_string, '&'); // [ // "toto.foo" => ["bar", "baz"], // "foo" => null, // "baz" => "", // ] ~~~

QueryParser::parse is not simlar to parse_str, QueryParser::extract is.

QueryParser::parse and QueryParser::extract both convert the query string into an array but QueryParser::parse logic don't result in data loss.

Since version 1.2.0 The alias function Uri\parse_query is available

~~~php ["bar", "baz"], // "foo" => null, // "baz" => "", // ] ~~~ ## QueryParser::convert

Available since version 1.5.0.

~~~php convert(['foo.bar' => ['2', 3, true]]); //or $arr = Uri\pairs_to_params(['foo.bar' => ['2', 3, true]]); //in both cases $arr = ['foo.bar' => 'true']; ~~~ ## QueryBuilder::build ~~~php By default the encoding is set to EncodingInterface::RFC3986_ENCODING

Since version 1.3.0 The method accepts any iterable construct.

~~~php parse($query_string, '&', Query::RFC3986_ENCODING); // $arr include the following data ["foo[]" => ['bar', 'baz']]; $res = $builder->build($arr, '&', false); // $res = 'foo[]=bar&foo[]=baz' ~~~

QueryBuilder::build is not similar to http_build_query.

Since version 1.2.0 The alias function Uri\build_query is available

Since version 1.3.0 The function accepts any iterable construct.

~~~php ['bar', 'baz']]; $res = Uri\build_query($arr, '&', false); // $res = 'foo[]=bar&foo[]=baz' ~~~