-1

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/

animuson
  • 53,861
  • 28
  • 137
  • 147
monde
  • 13
  • 3

3 Answers3

3

Try:

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

It will work if there is https instead of http

Test Here

Naveed
  • 41,517
  • 32
  • 98
  • 131
1

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;
?>
Sylter
  • 1,622
  • 13
  • 26
0

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.

Community
  • 1
  • 1
Taylor Evanson
  • 384
  • 4
  • 16