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]

[/php]

[php]
$currentDomain = $_SERVER[‘SERVER_NAME’];
[/php]

Don’t Miss : PHP GET_HEADERS

php get domain from url

[php]
$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]

php get domain name from url

[php]
parse_url(‘http://www.website.com/hey.php’, PHP_URL_HOST);
[/php]

How to Get Domain Name from URL in PHP?

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

echo getDomain(“https://itsolutionstuck.com”); // results ‘itsolutionstuck.com’
echo getDomain(“https://www.itsolutionstuck.com”); // results ‘itsolutionstuck.com’
echo getDomain(“http://mail.itsolutionstuck.co.uk”); // results ‘itsolutionstuck.co.uk’
[/php]

See also :  WordPress Themes: Automatically Deleting Deactivated Themes

Leave a Comment