PHP Get Domain With http

1.3k Views Asked by At

I want to get Get Domain from URL and be output: http://www.domain.com/

I found this, but does not come out with the http://

<?php
$url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
$parse = parse_url($url);
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
print $parse['host']; // prints 'google.com'
?>

Output: www.lebanonpost.com

I want it to be: http://www.lebanonpost.com/

3

There are 3 best solutions below

1
On BEST ANSWER

Try:

print $parse['scheme'] . '://' . $parse['host'];

It will work if there is https instead of http

Test Here

0
On

This is the resource I always use for printing url's with PHP - https://stackoverflow.com/a/8891890/1964113

This answer breaks down each piece, even http/https and #fragments.

Google these things man! Really easy to find.

1
On

You can concate http:// to your output:

<?php
    $url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
    $parse = parse_url($url);
    $domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
    $domainURL = $parse['scheme'].'://'.$parse['host'].'/';
    print $domainURL;
?>