2013-04-16 47 views
22

nói người nào đó nhập URL như sau:Nhận tên miền từ URL đầy đủ

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

Và tôi muốn quay trở lại: imgur.com

không i.imgur.com

Đây là mã tôi có ngay bây giờ

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

Nhưng điều này trả về i.imgur.com

+0

chăm sóc để chia sẻ ?? – ramo

+0

làm thế nào về một trong đó bắt đầu "Đây là một phương pháp để có được tên REAL của một tên miền." –

+0

Xem: http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url – duskwuff

Trả lời

61

Kiểm tra mã bên dưới, nó sẽ làm tốt công việc.

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

?> 
+0

Công trình này tuyệt vời! Cảm ơn bạn! Tôi sẽ đánh dấu đây là câu trả lời hay nhất. – ramo

+3

Đây là một chút * khéo léo. Nó sẽ kết thúc trả về kết quả không chính xác cho các tên miền phụ của ccTLD cấp đầu tiên, như 'blah.blah.de'. Nhưng không có cách nào xung quanh điều đó mà không sử dụng Danh sách Hậu tố Công khai. – duskwuff

+0

Điều này sẽ không hoạt động đối với 'www.domain.com' - parse_url() cần một giao thức (http: //) để xác định một máy chủ, nếu không nó chỉ là đường dẫn. Cần một điều kiện thứ cấp dưới đầu tiên: '$ domain = (empty ($ domain) && isset ($ pieces ['path']))? $ pieces ['path']: $ domain; ' –

-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]; 
} 
+0

Ummm ... đó là gì? –

+0

là chức năng nhận tên máy chủ từ url.url gốc là url của bạn và $ host_url trả về url máy chủ lưu trữ .. –

-6

sử dụng này:

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

Ví dụ:

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

Lệnh nhận được thư mục và không miền.

+0

Câu hỏi liên quan đến URL mà người dùng đã nhập, không phải địa chỉ họ đang truy cập. – robmcvey

-5

Nếu bạn chỉ muốn tên miền, hãy thử như sau:

$domain = $_SERVER['SERVER_NAME']; 

echo $domain; 
5

Bạn cần gói rằng việc sử dụng Public Suffix List. Có, bạn có thể sử dụng các hàm chuỗi arround parse_url() hoặc regex, nhưng chúng sẽ tạo ra kết quả không đúng trong các URL phức tạp.

tôi khuyên bạn nên TLDExtract cho phân tích miền, đây là mẫu mã:

$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' 
1

Tôi đã tìm thấy một thư viện rất hữu ích sử dụng publicsuffix.org, PHP Domain Parser là một phân tích cú pháp Phạm vi công cộng Suffix Danh sách dựa thực hiện trong PHP .

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"

Các vấn đề liên quan