How to get a domain name from a URL using PHP
In this tutorial, we will learn How to get a domain name from a URL using PHP. Simply get the domain name in a fine URL using PHP. Let’s say you have a string variable name URL that has the value of devnote.in.
$url = https://devnote.in/laravel/
Get Domain Name
The parse_url()
function by sending a URL and returning an array of the URL components. parse_url return three-parameter in host parameter is used to access the hostname, scheme parameter is used to access protocol and path parameter is used to access path. Now get the domain name from a URL using PHP, you can use the parse_url() function by default PHP provides.
<?php $url = "https://devnote.in/laravel"; $domain = parse_url($url); echo "Your domain is : ".$domain['scheme'].'://'.$domain['host'].$domain['path']; $url = "https://devnote.in/laravel"; $domain = parse_url($url); echo "Your domain is : ".$domain['host']; Output: Your domain is : devnote.in