How do you strip out the domain name from a URL in

2019-01-11 02:37发布

Im looking for a method (or function) to strip out the domain.ext part of any URL thats fed into the function. The domain extension can be anything (.com, .co.uk, .nl, .whatever), and the URL thats fed into it can be anything from http://www.domain.com to www.domain.com/path/script.php?=whatever

Whats the best way to go about doing this?

标签: php urlparse
7条回答
叼着烟拽天下
2楼-- · 2019-01-11 03:01

I spent some time thinking about whether it makes sense to use a regular expression for this, but in the end I think not.

firstresponder's regexp came close to convincing me it was the best way, but it didn't work on anything missing a trailing slash (so http://example.com, for instance). I fixed that with the following: '/\w+\..{2,3}(?:\..{2,3})?(?=[\/\W])/i', but then I realized that matches twice for urls like 'http://example.com/index.htm'. Oops. That wouldn't be so bad (just use the first one), but it also matches twice on something like this: 'http://abc.ed.fg.hij.kl.mn/', and the first match isn't the right one. :(

A co-worker suggested just getting the host (via parse_url()), and then just taking the last two or three array bits (split() on '.') The two or three would be based on a list of domains, like 'co.uk', etc. Making up that list becomes the hard part.

查看更多
趁早两清
3楼-- · 2019-01-11 03:01

There is only one correct way to extract domain parts, it's use Public Suffix List (database of TLDs). I recomend TLDExtract package, here is sample code:

$extract = new LayerShifter\TLDExtract\Extract();

$result = $extract->parse('www.domain.com/path/script.php?=whatever');
$result->getSubdomain(); // will return (string) 'www'
$result->getHostname(); // will return (string) 'domain'
$result->getSuffix(); // will return (string) 'com'
查看更多
贼婆χ
4楼-- · 2019-01-11 03:05

Here are a couple simple functions to get the root domain (example.com) from a normal or long domain (test.sub.domain.com) or url (http://www.example.com).

/**
 * Get root domain from full domain
 * @param string $domain
 */
public function getRootDomain($domain)
{
    $domain = explode('.', $domain);

    $tld = array_pop($domain);
    $name = array_pop($domain);

    $domain = "$name.$tld";

    return $domain;
}

/**
 * Get domain name from url
 * @param string $url
 */
public function getDomainFromUrl($url)
{
    $domain = parse_url($url, PHP_URL_HOST);
    $domain = $this->getRootDomain($domain);

    return $domain;
}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-11 03:09

You can use parse_url() to do this:

$url = 'http://www.example.com';
$domain = parse_url($url, PHP_URL_HOST);
$domain = str_replace('www.','',$domain);

In this example, $domain should contain example.com, irrespective of it having www or not. It also works for a domain such as .co.uk

查看更多
放我归山
6楼-- · 2019-01-11 03:12

parse_url turns a URL into an associative array:

php > $foo = "http://www.example.com/foo/bar?hat=bowler&accessory=cane";
php > $blah = parse_url($foo);
php > print_r($blah);
Array
(
    [scheme] => http
    [host] => www.example.com
    [path] => /foo/bar
    [query] => hat=bowler&accessory=cane
)
查看更多
ら.Afraid
7楼-- · 2019-01-11 03:15

Solved this...

Say we're calling dev.mysite.com and we want to extract 'mysite.com'

$requestedServerName = $_SERVER['SERVER_NAME']; // = dev.mysite.com

$thisSite = explode('.', $requestedServerName); // site name now an array

array_shift($thisSite); //chop off the first array entry eg 'dev'

$thisSite = join('.', $thisSite); //join it back together with dots ;)

echo $thisSite; //outputs 'mysite.com'

Works with mysite.co.uk too so should work everywhere :)

查看更多
登录 后发表回答