PHP parse_url() Function

Definition
In PHP, parse_url() splits a URL into parts: scheme, host, port, user, password, path, query and fragment.
You get an array of the parts that exist, or a single part if you ask for one.
Syntax
Call parse_url() with the values you need. Start with this line.
$parts = parse_url($url);
| Call | What it does |
|---|
parse_url("https://example.com/a?x=1") | Returns an array of the URL parts. |
parse_url($url, PHP_URL_HOST) | Returns only the host name. |
Only the parts found in the URL are returned. Check with isset() before you use one.
Library declarations
parse_url(string $url, int $component = -1): int|string|array|null|false
Parameters
| Parameter | Description |
|---|
| url | Required. The URL to split. |
| component | Optional. One of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT. |
Return Value
| Return Value | Description |
|---|
| array | An array of parts when no component is given. |
| string|int|null | The single part when a component is given. null if that part is missing. The port is an int. |
| false | Returned for a badly formed URL. |
PHP Examples
Example 1 — Split a URL into Parts
Break a full URL into an array.
<?php
$url = "https://user:[email protected]:8080/path/page.php?x=1&y=2#top";
print_r(parse_url($url));
Array
(
[scheme] => https
[host] => example.com
[port] => 8080
[user] => user
[pass] => pass
[path] => /path/page.php
[query] => x=1&y=2
[fragment] => top
)How It Works
- Pass a full URL. It has every part.
- Get an array. Each part has its own key.
- Notice the port. It is returned as a number.
- Skip missing parts. A URL without a fragment has no
fragment key.
Example 2 — Get One Part or Catch a Bad URL
Ask for a single part with a constant. A badly formed URL returns false.
<?php
$url = "https://example.com/blog/post";
echo parse_url($url, PHP_URL_HOST), PHP_EOL;
var_dump(parse_url($url, PHP_URL_QUERY));
var_dump(parse_url("http:///example.com"));
example.com
NULL
bool(false)
How It Works
- Pass
PHP_URL_HOST. You get just the host as a string. - Ask for a missing part. The query is not there, so you get
NULL. - Try a broken URL.
http:///example.com has three slashes. - Handle
false. It means PHP could not parse the URL.
Frequently Asked Questions
parse_url() splits a URL into its parts such as scheme, host, port, path, query and fragment and returns them as an array.
parse_url(string $url, int $component = -1): int|string|array|null|false. Pass the URL and, if you like, a PHP_URL_* constant.
Pass PHP_URL_HOST as the second argument: parse_url($url, PHP_URL_HOST).
It returns false for a seriously malformed URL, so always check the result before you use it.
Get the query with PHP_URL_QUERY and pass it to parse_str(), which fills an array with the values.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful