Question

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/

Was it helpful?

Solution

Try:

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

It will work if there is https instead of http

Test Here

OTHER TIPS

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;
?>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top