PHP

How to Get Domain Name From URL in PHP


In this article, you will learn how to get domain name from a URL in PHP.

Let’s say you have a string variable named ‘a’ that has the value of code-source.tempurl.host’s article URL.

$a = "https://codesource.io/javascript-design-patterns/";

Get Domain Name

In order to get the domain name from a URL, you can use the parse_url() method and host parameter.

$a = "https://codesource.io/javascript-design-patterns/";

$domain = parse_url($a);

echo "The domain is ".$domain['host'];
// The domain is code-source.tempurl.host

Note: The parse_url() method functions by parsing a URL and returning an array of the URL components. The host parameter is used to access the hostname of the supplied URL.


Share on social media

//