我想从URL获取域并输出: http://www.domain.com/

我发现了这个,但没有拿出 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'
?>

输出: www.lebanonpost.com

我希望它是: http://www.lebanonpost.com/

有帮助吗?

解决方案

试试:

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

它会工作,如果有 https 而不是 http

在这里测试

其他提示

您可以将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));
    $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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top