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
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:
|Name||Flag||Example Return Value|
|User for basic auth||PHP_URL_USER||admin|
|Password for basic auth||PHP_URL_PASS||pass|
By default, the function will return all parts as an array:
If you want to retrieve only the domain from a URL, you can use the PHP_URL_HOST flag. For example:
$domain = parse_url('https://haait.net/laravel', PHP_URL_HOST); //haait.net
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.
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:
use Spatie\Url\Url; $info = Url::fromString('https://haait.net/laravel'); $domain = $info->getHost(); //haait.net
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:firstname.lastname@example.org:80/category/laravel?name=1#section1.
|Method||Description||Example Return Value|
|getAuthority()||Returns auth info with host and port||admin:email@example.com:80|
|getUserInfo()||Returns user name and password for basic auth||admin:pass|
|getHost()||Returns site domain||haait.net|
|getPath()||Returns query path||/categories/laravel|
|getQuery()||Returns query parameters as string||name=1|
|getQueryParameter(‘name’)||Returns query parameter value by its name||1|
|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 path||categories|
|getLastSegment()||Returns the last part of the path||laravel|
|getFragment()||Returns fragment after hashtag||#section1|
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:
use League\Uri\Uri; $info = Uri::createFromString('https://admin:firstname.lastname@example.org:80/category/laravel?name=1#section1'); $domain = $info->getHost();
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.
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!