Parsing a URL

suggest change

To separate a URL into its individual components, use parse_url():

$url = 'http://www.example.com/page?foo=1&bar=baz#anchor';
$parts = parse_url($url);

After executing the above, the contents of $parts would be:

Array
(
    [scheme] => http
    [host] => www.example.com
    [path] => /page
    [query] => foo=1&bar=baz
    [fragment] => anchor
)

You can also selectively return just one component of the url. To return just the querystring:

$url = 'http://www.example.com/page?foo=1&bar=baz#anchor';
$queryString = parse_url($url, PHP_URL_QUERY);

Any of the following constants are accepted: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY and PHP_URL_FRAGMENT.

To further parse a query string into key value pairs use parse_str():

$params = [];
parse_str($queryString, $params);

After execution of the above, the $params array would be populated with the following:

Array
(
    [foo] => 1
    [bar] => baz
)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


URLs:
* URLs
* Parsing a URL

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR