Parsers

Since version 1.5.0

The library provides the following classes to ease components parsing:

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

QueryParser::extract

<?php

public QueryParser::extract(string $query[, string $separator = '&' [, int $enc_type = PHP_QUERY_RFC3986]]): array

This method deserializes the query string parameters into an associative array similar to PHP’s parse_str when used with its optional second argument. This static public method expects the following arguments:

The main differences are with parse_str usage are the following:

<?php

use League\Uri\QueryParser;

$query_string = 'foo.bar=bar&foo_bar=baz';
parse_str($query_string, $out);
var_export($out);
// $out = ["foo_bar" => '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

use League\Uri;

$query_string = 'foo.bar=bar&foo_bar=baz';
parse_str($query_string, $out);
var_export($out);
// $out = ["foo_bar" => 'baz'];

$arr = Uri\extract_query($query_string);
// $arr = ['foo.bar' => 'bar', 'foo_bar' => baz'];

QueryParser::parse

<?php

public QueryParser::parse(string $query[, string $separator = '&' [, int $enc_type = PHP_QUERY_RFC3986]]): array

This method parse the query string into an associative array of key/values pairs. The method expects three (3) arguments:

The value returned for each pair can be:

<?php

use League\Uri;

$parser = new QueryParser();

$query_string = 'toto.foo=bar&toto.foo=baz&foo&baz=';
$arr = $parser->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

use League\Uri;

$query_string = 'toto.foo=bar&toto.foo=baz&foo&baz=';
$arr = Uri\parse_query($query_string, '&');
// [
//     "toto.foo" => ["bar", "baz"],
//     "foo" => null,
//     "baz" => "",
// ]

QueryParser::convert

Available since version 1.5.0.

<?php

public QueryParser::convert(iterable $pairs): array

//function alias

function pairs_to_params(iterable $pairs): array

The QueryParser::convert deserializes a collection of query string key/value pairs into an associative array similar to PHP’s parse_str when used with its optional second argument. This method expects a single argument which represents the collection of key/value pairs as an iterable construct.

The main differences are with parse_str usage are the following:

You can also use the function alias which is Uri\pairs_to_params.

<?php

use League\Uri;

$parser = new Uri\QueryParser();
$arr = $parser->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

public QueryBuilder::build(iterable $pairs[, string $separator = '&' [, int $enc_type = PHP_QUERY_RFC3986]]): array

The QueryBuilder::build restores the query string from the result of the QueryBuilder::parse method. The method expects at most 3 arguments:

By default the encoding is set to EncodingInterface::RFC3986_ENCODING

Since version 1.3.0 The method accepts any iterable construct.

<?php

use League\Uri\QueryBuilder;
use League\Uri\QueryParser;

$builder = new QueryBuilder();
$parser = new QueryParser();


$query_string = 'foo[]=bar&foo[]=baz';
$arr = $parser->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

use League\Uri;

$query_string = 'foo[]=bar&foo[]=baz';
$arr = Query::parse($query_string, '&', Query::RFC3986_ENCODING);
var_export($arr);
// $arr include the following data ["foo[]" => ['bar', 'baz']];

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