PHP parse_url() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

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.

PHP
$parts = parse_url($url);
CallWhat 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
PHP
parse_url(string $url, int $component = -1): int|string|array|null|false

Parameters

ParameterDescription
urlRequired. The URL to split.
componentOptional. 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 ValueDescription
arrayAn array of parts when no component is given.
string|int|nullThe single part when a component is given. null if that part is missing. The port is an int.
falseReturned for a badly formed URL.

PHP Examples

Example 1 — Split a URL into Parts

Break a full URL into an array.

PHP
<?php
$url = "https://user:[email protected]:8080/path/page.php?x=1&y=2#top";

print_r(parse_url($url));

How It Works

  1. Pass a full URL. It has every part.
  2. Get an array. Each part has its own key.
  3. Notice the port. It is returned as a number.
  4. 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
<?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"));

How It Works

  1. Pass PHP_URL_HOST. You get just the host as a string.
  2. Ask for a missing part. The query is not there, so you get NULL.
  3. Try a broken URL. http:///example.com has three slashes.
  4. Handle false. It means PHP could not parse the URL.

Example 3 — Read Query Values with parse_str()

Combine parse_url() and parse_str() to read the query as an array.

PHP
<?php
$url = "https://example.com/list?page=2&sort=name";

$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);

print_r($params);

How It Works

  1. Get the query text. PHP_URL_QUERY returns page=2&sort=name.
  2. Call parse_str(). It splits the text into key and value pairs.
  3. Read the array. The values are strings.
  4. Use the values. For example $params["page"] is 2.

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.

Next: rawurldecode()

rawurldecode() decodes %XX sequences in a URL without changing plus signs.

rawurldecode() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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