Skip to content

Core\Uri::parseQuery

Core\Uri::parseQuery(string $query): array<mixed>Core\Uri::parseQuery(
    string $query,
): array<mixed>

Reads a query string into an array. The query string is the part of a link after the ?, such as q=red+shoes&page=2.

Each pair gives one name and one value. Both are decoded, so + becomes a space and %3A becomes :. Each value is bytes, because an escape can give any byte. Use as string to get text. When a name appears twice, the last value is kept.

Brackets in a name build nested arrays. size[]=M&size[]=L gives a list with two values, and filter[color]=red gives an array with the key color. Core\Uri::buildQuery writes the array back as text.

The examples below read a search query, read lists and nested values, and read the page number of a list.

Parameters

  • $querystringThe query text, without the ? at its start.

Return value

Returns array<mixed>. An array. Each value is bytes or another array. A value is bytes because an escape can give any byte, and as string converts it to text. A pair without = has an empty value. A pair with an empty name is left out. When a name appears twice without brackets, the last value is kept.

Errors

  • RuntimeErrorA name decodes to bytes that are not valid UTF-8. A name is a key of the array, and a key must be text. A value can contain any bytes.

Examples

Read a search query

<?nvs
// Shows how to read the names and values of a query string.
array<mixed> $params = Core\Uri::parseQuery('q=red+shoes&page=2&sort=price%3Adesc');
// Each value is decoded, so `+` becomes a space and `%3A` becomes `:`.
// `as string` converts each value to text. This prints red shoes, 2 and price:desc.
echo $params['q'] as string, "\n";
echo $params['page'] as string, "\n";
echo $params['sort'] as string, "\n";
// A name that appears twice keeps its last value. This prints 3.
echo Core\Uri::parseQuery('page=2&page=3')['page'] as string, "\n";
Output
red shoes
2
price:desc
3

Lists and nested values

<?nvs
// Shows how brackets in the names of a query string build lists and nested arrays.
array<mixed> $params = Core\Uri::parseQuery('size[]=M&size[]=L&filter[color]=red&filter[brand]=acme');
// `size[]` adds one value to a list each time. This prints 2, then M and L.
array<mixed> $sizes = $params['size'] as array<mixed>;
echo Core\Arr::count($sizes), "\n";
foreach ($sizes as mixed $size) {
echo $size as string, "\n";
}
// `filter[color]` puts the value under the key `color`. This prints red and acme.
array<mixed> $filter = $params['filter'] as array<mixed>;
echo $filter['color'] as string, "\n";
echo $filter['brand'] as string, "\n";
Output
2
M
L
red
acme

Read the page number of a list

<?nvs
// Shows how a server reads the page number of a list from the query of a link.
class Listing
{
public static function page(string $query): int
{
array<mixed> $params = Core\Uri::parseQuery($query);
if (!Core\Arr::hasKey($params, 'page')) {
return 1;
}
// `as ?int` gives `null` when the text is not a whole number, so page 1 is used.
int $page = (($params['page'] as string) as ?int) ?? 1;
if ($page < 1) {
return 1;
}
return $page;
}
}
// In a real program, the query comes from the link the visitor opened.
// This prints 4, 1, 1 and 1.
array<string> $queries = ['page=4&sort=new', 'sort=new', 'page=abc', 'page=-3'];
foreach ($queries as string $query) {
echo Listing::page($query), "\n";
}
Output
4
1
1
1