简体   繁体   English

如何从URL获取Domain.ext? PHP的

[英]How can I get the Domain.ext from url? PHP prase

if we have 如果我们有

 $url = "http://subdomain.domin.ext/somepath/file.php";

how to get the domain.ext 如何获得domain.ext

when I used 当我用

 $parse = parse_url($url);
 $domin = $parse[host];

then echo "$domain"; //this returns subdomain.domin.ext 然后echo "$domain"; //this returns subdomain.domin.ext echo "$domain"; //this returns subdomain.domin.ext

is there any idea to get domain.ext? 有什么想法要得到domain.ext吗?

$url = "http://subdomain.domin.ext/somepath/file.php";
$parse = parse_url($url);
$domain = $parse['host'];
$lastDot = strrpos($domain,'.');
$ext = substr($domain,$lastDot+1);

This will get you "ext" (eg "com" or "gov"). 这将使您“分机”(例如“ com”或“ gov”)。 In the case of something like amazon.co.uk, it will give you "uk". 在诸如amazon.co.uk之类的情况下,它将给您“英国”。 If you need "co.uk", you will need to add logic to detect certain TLDs (eg "uk") and go to the second dot on those cases. 如果需要“ co.uk”,则需要添加逻辑以检测某些TLD(例如“ uk”),然后转到这些情况下的第二个点。

If you have a specific domain with different TLDs, like mysite with .co.uk and .com , than you can basically do the following: 如果您拥有一个具有不同TLD的特定域,例如mysite.co.uk.com ,则基本上可以执行以下操作:

$url = 'http://mysite.co.uk/some-dir/';
$domain_pos = strpos($url, 'mysite');
$domain = substr($url, $domain_pos);
$ext_pos = strpos($domain, '/');
if($ext_pos !== false) { // extra check for paths/sub-dirs
  $domain = substr($domain, 0, $ext_pos); 
}
echo $domain;

Returns mysite.co.uk or whatever TLD that gets assigned to the $url . 返回mysite.co.uk或分配给$url任何TLD。

Note the extra check for paths and sub directories. 请注意对路径和子目录的额外检查。 :) :)

<?php
$url = "http://subdomain.domain.ext/somepath/file.php";
$parse = parse_url($url);
$domain = substr(strrchr($parse['host'], '.'), 1);
$domain = substr(strrchr(substr($parse['host'], 0, -strlen($domain)), '.'), 1). '.'. $domain;
if(in_array(strstr($domain, '.', true), array('co', 'com', 'net', 'org', 'edu', 'gov', 'mil')))
{
  $domain = substr(strrchr(substr($parse['host'], 0, -strlen($domain)), '.'), 1). '.'. $domain;
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM