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:



Table Of Contents