Last active
March 15, 2018 15:56
-
-
Save shrikus/3e463c5b399d285e53cfbf15dc565415 to your computer and use it in GitHub Desktop.
PHP code to get the domain name without subdomains (includes the tld, and the special types from IANA). Don't have support for unicode domain names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $domain Pass $_SERVER['SERVER_NAME'] here | |
* | |
* @return string | |
* | |
* replacement for https://gist.github.com/pocesar/5366899 | |
*/ | |
function get_domain_base($domain) { | |
$original = $domain = strtolower($domain); | |
if (filter_var($domain, FILTER_VALIDATE_IP)) { return $domain; } | |
$tlds = array('aero','arpa','asia','biz','cat','com','coop','edu','gov','info','jobs','mil','mobi','museum','name','net','org','post','pro','tel','travel', 'xxx'); | |
$tlds_2 = array('govt','idf','k12','law','ltd','mod','muni','nom','nhs','parliament','plc','school','sch'); | |
$out = array(); | |
$arr = array_reverse(explode('.', $domain)); | |
foreach($arr as $sub) { | |
if (strlen($sub)==2 && count($out) < 2) $out[] = $sub; | |
elseif (in_array($sub, $tlds)) $out[] = $sub; | |
elseif (in_array($sub, $tlds_2) && count($out)==1) $out[] = $sub; | |
else { | |
$out[] = $sub; | |
break; | |
} | |
} | |
return join('.', array_reverse($out)); | |
} | |
$urls = array( | |
'www.forum.example.ru' => 'example.ru', | |
'www.forum.org.ru' => 'forum.org.ru', | |
'www.forum.test.org.ru' => 'test.org.ru', | |
'www.example.com' => 'example.com', | |
'example.com' => 'example.com', | |
'example.com.br' => 'example.com.br', | |
'www.example.com.br' => 'example.com.br', | |
'www.example.gov.br' => 'example.gov.br', | |
'localhost' => 'localhost', | |
'www.localhost' => 'localhost', | |
'subdomain.localhost' => 'localhost', | |
'www.subdomain.example.com' => 'example.com', | |
'subdomain.example.com' => 'example.com', | |
'subdomain.example.com.br' => 'example.com.br', | |
'www.subdomain.example.com.br' => 'example.com.br', | |
'www.subdomain.example.biz.br' => 'example.biz.br', | |
'subdomain.example.biz.br' => 'example.biz.br', | |
'subdomain.example.net' => 'example.net', | |
'www.subdomain.example.net' => 'example.net', | |
'www.subdomain.example.co.kr' => 'example.co.kr', | |
'subdomain.example.co.kr' => 'example.co.kr', | |
'example.co.kr' => 'example.co.kr', | |
'example.jobs' => 'example.jobs', | |
'www.example.jobs' => 'example.jobs', | |
'subdomain.example.jobs' => 'example.jobs', | |
'insane.subdomain.example.jobs' => 'example.jobs', | |
'insane.subdomain.example.com.br' => 'example.com.br', | |
'www.doubleinsane.subdomain.example.com.br' => 'example.com.br', | |
'www.subdomain.example.jobs' => 'example.jobs', | |
'test' => 'test', | |
'www.test' => 'test', | |
'subdomain.test' => 'test', | |
'www.detran.sp.gov.br' => 'sp.gov.br', | |
'www.mp.sp.gov.br' => 'sp.gov.br', | |
'ny.library.museum' => 'library.museum', | |
'www.ny.library.museum' => 'library.museum', | |
'ny.ny.library.museum' => 'library.museum', | |
'www.library.museum' => 'library.museum', | |
'info.abril.com.br' => 'abril.com.br', | |
'www.example.school.za' => 'example.school.za', | |
'www.example.asd.za' => 'asd.za', | |
'127.0.0.1' => '127.0.0.1', | |
'www.mod.mod.uk' => 'mod.mod.uk', | |
'::1' => '::1', | |
'dev.example.ca' => 'example.ca', | |
'dev.example.io' => 'example.io', | |
'sub.example.co' => 'example.co' | |
); | |
$failed = 0; | |
$total = count($urls); | |
foreach ($urls as $from => $expected) { | |
$from = get_domain_base($from); | |
if ($from !== $expected) { | |
$failed++; | |
print("<div style='color:red;'>expected {$from} to be {$expected}, got {$from}</div>"); | |
} | |
} | |
if ($failed) { | |
print("{$failed} tests failed out of {$total}"); | |
} else { | |
print("Success"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment