--- layout: default title: The Query component redirect_from: - /5.0/components/parsers/ --- Parsers =======
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. ## QueryParser::extract ~~~php 'baz']; $parser = new QueryParser(); $arr =$parser->extract($query_string); // $arr = ['foo.bar' => 'bar', 'foo_bar' => baz']; ~~~ ~~~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" => "", // ] ~~~ ~~~php ["bar", "baz"], // "foo" => null, // "baz" => "", // ] ~~~ ## QueryParser::convert ~~~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 toEncodingInterface::RFC3986_ENCODING
~~~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'
~~~
~~~php
['bar', 'baz']];
$res = Uri\build_query($arr, '&', false);
// $res = 'foo[]=bar&foo[]=baz'
~~~