PHP 8 reference · String Functions
PHP parse_str() Function
Parse a query string such as a=1&b=2 into an array, the reverse of http_build_query(). The result argument is required.
PHP 8 Since 4.0
Syntax
parse_str(string $string, array &$result): void
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
result |
The array that receives the variables. |
Return value
Nothing; the values go into result.
Examples
<?php
parse_str("page=2&sort=name&tags[]=php&tags[]=web", $params);
print_r($params);
Output
Array
(
[page] => 2
[sort] => name
[tags] => Array
(
[0] => php
[1] => web
)
)<?php
$url = "https://example.com/search?q=tea+%26+cake&page=3";
parse_str(parse_url($url, PHP_URL_QUERY), $query);
echo $query["q"], " / ", $query["page"], "\n";
Output
tea & cake / 3
Version history
Related
http_build_query()· Build a URL-encoded query string, such as page=2&sort=name, from an array. Nested arrays become key[]=... pairs.urlencode()· Encode a string for a URL query value: spaces become + and special characters become %XX codes.- PHP Strings · tutorial