parse_str
Parses a query string into an array.
Syntax
parse_str(string $string, array &$result): voidParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | A string like `a=1&b=2`. |
| $result | array | Receives the result. |
Return value
voidThe function returns nothing.
Examples
Parameters from an address:
<?php
parse_str(parse_url('https://example.com/search?q=php+book&tags[]=new&tags[]=sale', PHP_URL_QUERY), $params);
echo json_encode($params);Output:
{"q":"php book","tags":["new","sale"]}Notes
Values are decoded automatically, and square brackets in names turn into arrays.
See also
- http_build_query — Builds a URL query string from an array.
- parse_url — Splits a URL into its parts.
- urldecode — Decodes a string from a URL.
Related recipes
In the tutorial
- HTTP requests and responses — How PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.