fetch domain name from url

Get Domain Name from URL in PHP

Welcome back to shortlearner.com, in our previous post we learn how to send an attachment with the help of PHP and MySQL. today in this post we learn how to Get Domain Name from URL in PHP.

fetch domain name from url

so Recently I am working on one of my clients project and i need to fetch the domain name from the URL. This is a very small task but it is very useful stuff to learn.

Also Read
How to Install PHP on CentOS.
How to integrate Razorpay Payment Gateway using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

In this short article, We will write simple and easy code to get the domain name from URL by using some predefined PHP function.

First we need to parse URL using parse_url(). so we can get the array from parse URL and now we can fetch the specific host from the array using $parse[‘host’].

so in the below example we will find the domain name from a predefined URL which are in our $url variable. so copy the below code and run it on your server so you will found the domain name from a long URL.

<?php
$url = "http://localhost/wordpress";
$parse = parse_url($url);
$host = $parse['host'];
$host = str_ireplace('www.', '', $host);
echo $host;
?>