从完整网址获取域名

时间:2022-08-23 10:37:42

say someone enters a URL like this:

说有人输入这样的URL:

http://i.imgur.com/a/b/c?query=value&query2=value

And I want to return: imgur.com

我想回复:imgur.com

not i.imgur.com

不是i.imgur.com

This is code I have right now

这是我现在的代码

$sourceUrl = parse_url($url);
$sourceUrl = $sourceUrl['host'];

But this returns i.imgur.com

但这会返回i.imgur.com

7 个解决方案

#1


66  

Check the code below, it should do the job fine.

检查下面的代码,它应该做得很好。

<?php

function get_domain($url)
{
  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return $regs['domain'];
  }
  return false;
}

print get_domain("http://mail.somedomain.co.uk"); // outputs 'somedomain.co.uk'

?>

#2


6  

You need package that using Public Suffix List. Yes, you can use string functions arround parse_url() or regex, but they will produce incorrect result in complex URLs.

您需要使用Public Suffix List的包。是的,您可以使用字符串函数arround parse_url()或regex,但它们会在复杂的URL中产生错误的结果。

I recomend TLDExtract for domain parsing, here is sample code:

我建议使用TLDExtract进行域解析,这里是示例代码:

$url = 'http://i.imgur.com/a/b/c?query=value&query2=value';

parse_url($url, PHP_URL_HOST); // will return 'i.imgur.com'

$extract = new LayerShifter\TLDExtract\Extract();
$result = $extract->parse($url);
$result->getFullHost(); // will return 'i.imgur.com'
$result->getSubdomain(); // will return 'i'
$result->getRegistrableDomain(); // will return 'imgur.com'
$result->getSuffix(); // will return 'com'

#3


2  

I have found a very useful library using publicsuffix.org, PHP Domain Parser is a Public Suffix List based domain parser implemented in PHP.

我使用publicsuffix.org找到了一个非常有用的库,PHP Domain Parser是一个用PHP实现的基于Public Suffix List的域解析器。

https://github.com/jeremykendall/php-domain-parser

https://github.com/jeremykendall/php-domain-parser

 <?php 
 // this will do the job

 require_once '../vendor/autoload.php';

 $pslManager = new Pdp\PublicSuffixListManager();
 $parser = new Pdp\Parser($pslManager->getList());
 var_dump($parser->getRegistrableDomain('www.scottwills.co.uk'));
 ?>

string(16) "scottwills.co.uk"

string(16)“scottwills.co.uk”

#4


0  

The code below should be perfect for the job.

下面的代码应该是完美的工作。

function get_domain($url){
  $charge = explode('/', $url);
  $charge = $charge[2]; //assuming that the url starts with http:// or https://
  return $url;
}

echo get_domain('http://www.example.com/example.php');

#5


-1  

     if(substr_count($original_url, 'http://')) {
    if(substr_count($original_url, 'www.')) {
        // url style would be 'http://www.abc.xxx/page?param' or http://www.abc.xxx.xx/page?param
        // extract 'abc'
        $temp = explode('.', $original_url);

        $store_url = $temp[1];
        // now 
        // $temp[2] = xxx or xxx/page?param 
        // $temp[3] = null or xx/page?param 

        //if ($temp[3] == null) { // then we are sure that $temp[2]== "xxx/page?param"
                    if(sizeof($temp) > 3) {
            // extract "xxx" from "xxx/page?param" and append to store url so it will be "abc.xxx"  
            $temp = explode('/',$temp[2]);
            $store_url .= '.'.$temp[0];
        }
        else { 
            // then we are sure that $temp[2]== "xxx" and then $temp[3] == "xx/page?param"
            //                  or   $temp[2]== xxx/page?stripped-link from second dot(.)
            if(substr_count($temp[2], '/')) { // in case  $temp[2]== xxx/page?stripped-link from second dot(.)
                // extract "xxx" from "xxx/page?stripped-link" and appent to store url so it will be "abc.xxx"
                $temp = explode('/',$temp[2]);
                $store_url .= '.'.$temp[0]; // "abc".="xxx" ==> abc.xxx
            }
            else { // in case $temp[2]== "xxx" and then $temp[3] == "xx/page?param"
                $store_url .= '.'.$temp[2]; // "abc".="xxx" ==> abc.xxx
                // extract "xx" from "xx/page?param" and appent to store url so it will be "abc.xxx.xx"
                $temp = explode('/',$temp[3]);
                if(strlen($temp[0])==2) {
                    $store_url .= '.'.$temp[0];
                }
            }
        }
    }
    else {
        // url style would be 'http://abc.xxx/page?param' or 'http://abc.xxx.xx/page?param'
        // remove 'http://'
        $temp = substr($original_url, 7);
        // now temp would be either 'abc.xxx/page?param' or 'abc.xxx.xx/page?param'
        // explode with '/'
        $temp = explode('/', $temp);
        $store_url = $temp[0];
    }
}
else if(substr_count($original_url, 'www.')) {
    // url style would be 'www.abc.xxx/page?param' or 'www.abc.xxx.xx/page?param'
    // remove 'www.'
    $temp = substr($original_url, 4);
    // now, $temp would be either "abc.xxx/page?param" or "abc.xxx.xx/page?param"
    // explode with '/'
    $temp = explode('/', $temp);
    $store_url = $temp[0];
}
else {
    // url style would be 'abc.xxx/page?param' or 'abc.xxx.xx/page?param'
    //explode with '/'
    $temp = explode('/', $original_url);
    $store_url = $temp[0];
}

#6


-5  

use this:

用这个:

$uri = "$_SERVER[REQUEST_URI]";<br>
print($uri);

Example:

例:

http://exemple.com/?directory<br>
Result:
/?diretory

The command get the directory and not domain.

该命令获取目录而不是域。

#7


-5  

If you only want the domain name, try the following:

如果您只想要域名,请尝试以下操作:

$domain = $_SERVER['SERVER_NAME'];

echo $domain;

#1


66  

Check the code below, it should do the job fine.

检查下面的代码,它应该做得很好。

<?php

function get_domain($url)
{
  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path'];
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return $regs['domain'];
  }
  return false;
}

print get_domain("http://mail.somedomain.co.uk"); // outputs 'somedomain.co.uk'

?>

#2


6  

You need package that using Public Suffix List. Yes, you can use string functions arround parse_url() or regex, but they will produce incorrect result in complex URLs.

您需要使用Public Suffix List的包。是的,您可以使用字符串函数arround parse_url()或regex,但它们会在复杂的URL中产生错误的结果。

I recomend TLDExtract for domain parsing, here is sample code:

我建议使用TLDExtract进行域解析,这里是示例代码:

$url = 'http://i.imgur.com/a/b/c?query=value&query2=value';

parse_url($url, PHP_URL_HOST); // will return 'i.imgur.com'

$extract = new LayerShifter\TLDExtract\Extract();
$result = $extract->parse($url);
$result->getFullHost(); // will return 'i.imgur.com'
$result->getSubdomain(); // will return 'i'
$result->getRegistrableDomain(); // will return 'imgur.com'
$result->getSuffix(); // will return 'com'

#3


2  

I have found a very useful library using publicsuffix.org, PHP Domain Parser is a Public Suffix List based domain parser implemented in PHP.

我使用publicsuffix.org找到了一个非常有用的库,PHP Domain Parser是一个用PHP实现的基于Public Suffix List的域解析器。

https://github.com/jeremykendall/php-domain-parser

https://github.com/jeremykendall/php-domain-parser

 <?php 
 // this will do the job

 require_once '../vendor/autoload.php';

 $pslManager = new Pdp\PublicSuffixListManager();
 $parser = new Pdp\Parser($pslManager->getList());
 var_dump($parser->getRegistrableDomain('www.scottwills.co.uk'));
 ?>

string(16) "scottwills.co.uk"

string(16)“scottwills.co.uk”

#4


0  

The code below should be perfect for the job.

下面的代码应该是完美的工作。

function get_domain($url){
  $charge = explode('/', $url);
  $charge = $charge[2]; //assuming that the url starts with http:// or https://
  return $url;
}

echo get_domain('http://www.example.com/example.php');

#5


-1  

     if(substr_count($original_url, 'http://')) {
    if(substr_count($original_url, 'www.')) {
        // url style would be 'http://www.abc.xxx/page?param' or http://www.abc.xxx.xx/page?param
        // extract 'abc'
        $temp = explode('.', $original_url);

        $store_url = $temp[1];
        // now 
        // $temp[2] = xxx or xxx/page?param 
        // $temp[3] = null or xx/page?param 

        //if ($temp[3] == null) { // then we are sure that $temp[2]== "xxx/page?param"
                    if(sizeof($temp) > 3) {
            // extract "xxx" from "xxx/page?param" and append to store url so it will be "abc.xxx"  
            $temp = explode('/',$temp[2]);
            $store_url .= '.'.$temp[0];
        }
        else { 
            // then we are sure that $temp[2]== "xxx" and then $temp[3] == "xx/page?param"
            //                  or   $temp[2]== xxx/page?stripped-link from second dot(.)
            if(substr_count($temp[2], '/')) { // in case  $temp[2]== xxx/page?stripped-link from second dot(.)
                // extract "xxx" from "xxx/page?stripped-link" and appent to store url so it will be "abc.xxx"
                $temp = explode('/',$temp[2]);
                $store_url .= '.'.$temp[0]; // "abc".="xxx" ==> abc.xxx
            }
            else { // in case $temp[2]== "xxx" and then $temp[3] == "xx/page?param"
                $store_url .= '.'.$temp[2]; // "abc".="xxx" ==> abc.xxx
                // extract "xx" from "xx/page?param" and appent to store url so it will be "abc.xxx.xx"
                $temp = explode('/',$temp[3]);
                if(strlen($temp[0])==2) {
                    $store_url .= '.'.$temp[0];
                }
            }
        }
    }
    else {
        // url style would be 'http://abc.xxx/page?param' or 'http://abc.xxx.xx/page?param'
        // remove 'http://'
        $temp = substr($original_url, 7);
        // now temp would be either 'abc.xxx/page?param' or 'abc.xxx.xx/page?param'
        // explode with '/'
        $temp = explode('/', $temp);
        $store_url = $temp[0];
    }
}
else if(substr_count($original_url, 'www.')) {
    // url style would be 'www.abc.xxx/page?param' or 'www.abc.xxx.xx/page?param'
    // remove 'www.'
    $temp = substr($original_url, 4);
    // now, $temp would be either "abc.xxx/page?param" or "abc.xxx.xx/page?param"
    // explode with '/'
    $temp = explode('/', $temp);
    $store_url = $temp[0];
}
else {
    // url style would be 'abc.xxx/page?param' or 'abc.xxx.xx/page?param'
    //explode with '/'
    $temp = explode('/', $original_url);
    $store_url = $temp[0];
}

#6


-5  

use this:

用这个:

$uri = "$_SERVER[REQUEST_URI]";<br>
print($uri);

Example:

例:

http://exemple.com/?directory<br>
Result:
/?diretory

The command get the directory and not domain.

该命令获取目录而不是域。

#7


-5  

If you only want the domain name, try the following:

如果您只想要域名,请尝试以下操作:

$domain = $_SERVER['SERVER_NAME'];

echo $domain;