Home » PHP » How to Get Domain From URL in PHP

How to Get Domain From URL in PHP

Sometimes, you may need to extract the domain or some other parts from an URL. It is a very straightforward task that can be done using the built-in parse_url() function without any external libraries. However, third-party libraries can provide more convenience and power to handle URLs

In this short article, I will show how to get domain from URL in PHP using both the built-in function and object-oriented third-party libraries.

Table of Contents

How to Get Domain From URL in PHP


1. parse_url()

The parse_url() function splits the URL from the first argument into different parts. The second argument can be used to specify which parts you want to retrieve. Here is the list of available URL parts and flags that you can use as the second argument:

NameFlagExample Return Value
User for basic authPHP_URL_USERadmin
Password for basic authPHP_URL_PASSpass

By default, the function will return all parts as an array:

index.php$parts = parse_url('https://haait.net/laravel'); dump($parts);

If you want to retrieve only the domain from a URL, you can use the PHP_URL_HOST flag. For example:

index.php$domain = parse_url('https://haait.net/laravel', PHP_URL_HOST); //haait.net dump($domain);

This approach may not be the most convenient, because you have to know each part name to retrieve them from an array. But you can use one of the packages that do the same thing more fluently.

2. spatie/url

The spatie/url package is a simple wrapper for the parse_url() function. It does not require any PHP extensions, and implements UriInterface from PSR-7, allowing you to easily retrieve the domain name and other parts of a URL. You can install it using Composer:

composer require spatie/url

Then, use the Spatie\Url\Url class to get domain from URL:

index.phpuse Spatie\Url\Url; $info = Url::fromString('https://haait.net/laravel'); $domain = $info->getHost(); //haait.net dump($domain);

Here you can use the URL object and its method getHost() which can be auto-completed by IDE. Also, this library has other methods, that make working with URLs more convenient. You can see the most useful of them in the table below. The URL for all examples from the table is https://admin:pass@haait.net:80/category/laravel?name=1#section1.

MethodDescriptionExample Return Value
getScheme()Returns schemehttps
getAuthority()Returns auth info with host and portadmin:pass@haait.net:80
getUserInfo()Returns user name and password for basic authadmin:pass
getHost()Returns site domainhaait.net
getPort()Returns port80
getPath()Returns query path/categories/laravel
getQuery()Returns query parameters as stringname=1
getQueryParameter(‘name’)Returns query parameter value by its name1
getAllQueryParameters()Returns all query parameters as an array[‘name’ => 1]
getSegments()Returns parts of the path as an array[‘categories’, ‘laravel’]
getFirstSegment()Returns the first part of the pathcategories
getLastSegment()Returns the last part of the pathlaravel
getFragment()Returns fragment after hashtag#section1

3. league/uri

ThePHPLeuague provides a robust and powerful package called league/uri, designed for creating and parsing complicated URLs. It implements UriInterface from PSR-7, and also supports URL templates and i18n URLs. You can install it using the following command:

composer require league/uri

While it may not be necessary to install this library for parsing simple URLs, if it is already installed in your project, you can certainly use it. In any case, to get the domain from a URL using this library, you can use the following code:

index.phpuse League\Uri\Uri; $info = Uri::createFromString('https://admin:pass@haait.net:80/category/laravel?name=1#section1'); $domain = $info->getHost(); dump($domain);

As mentioned earlier, this library supports all the methods for getting information about URL from UrlInterface, and I will not repeat them in this section.

Wrapping Up

In this short article, I have shown three ways to fetch domain from URL in PHP. Which way do you prefer? Are there any other useful libraries for this purpose that you know of? Please share your thoughts in the comments section below!

Leave a Comment

Exit mobile version