get domain name in php – Extract domain, path etc from a full url with PHP

get domain name in php – How to get current page URL in PHP? All PHP code are group together in getDomain() function.

get domain name in php

In order to get the domain name from a URL, you can use the parse_url() method and host parameter. also you can more details for get domain name in php

PHP
PHP

php get current domain

<?php  
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')   
         $url = "https://";   
    else  
         $url = "http://";   
    // Append the host(domain name, ip) to the URL.   
    $url.= $_SERVER['HTTP_HOST'];   
    
    // Append the requested resource location to the URL   
    $url.= $_SERVER['REQUEST_URI'];    
      
    echo $url;  
  ?>   

$currentDomain = $_SERVER['SERVER_NAME'];

Don’t Miss : PHP GET_HEADERS

php get domain from url

$url = 'https://www.itsolutionstuck.com/how-to-send-attachment-in-mail-in-php.html';
$parse = parse_url($url);
echo $parse['host']; // prints 'itsolutionstuck.com'

php get domain name from url

parse_url('http://www.website.com/hey.php', PHP_URL_HOST);

How to Get Domain Name from URL in PHP?

function getDomain($url){
    $pieces = parse_url($url);
    $domain = isset($pieces['host']) ? $pieces['host'] : '';
    if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)){
        return $regs['domain'];
    }
    return FALSE;
}

echo getDomain("http://itsolutionstuck.com"); // results 'itsolutionstuck.com'
echo getDomain("http://www.itsolutionstuck.com"); // results 'itsolutionstuck.com'
echo getDomain("http://mail.itsolutionstuck.co.uk"); // results 'itsolutionstuck.co.uk'

See also :  is_array in php - PHP is_array() Examples

Leave a Reply

Your email address will not be published.