PHP gets the top level domain function code for the url

  • 2020-05-26 07:55:54
  • OfStack

PHP gets the top-level domain function for a url

Currently, the international top-level domains are: com|, edu|, gov|, int|, mil|, net|, org|, org|, name |0, coop |1, aero |3, xxx |5, cc |7, me
Regional domain name is more, should be every country and region will have, you can go to collect, but as far as I know, this domain name is composed of two letters, it can be used alone or drink international top-level domain name combined with regional domain name, such as:
cn China
tw Taiwan
hk Hong Kong

Domain name example:
jb51.cn
baidu.com
jb51.com.cn

Take jb51.dom.cn as an example: phpwind (custom part).com (international domain name part).cn (regional domain name part)

Gets the PHP function for the top-level domain
 
function getdomain($url) { 
$host = strtolower ( $url ); 
if (strpos ( $host, '/' ) !== false) { 
$parse = @parse_url ( $host ); 
$host = $parse ['host']; 
} 
$topleveldomaindb = array ('com', 'edu', 'gov', 'int', 'mil', 'net', 'org', 'biz', 'info', 'pro', 'name', 'museum', 'coop', 'aero', 'xxx', 'idv', 'mobi', 'cc', 'me' ); 
$str = ''; 
foreach ( $topleveldomaindb as $v ) { 
$str .= ($str ? '|' : '') . $v; 
} 

$matchstr = "[^\.]+\.(?:(" . $str . ")|\w{2}|((" . $str . ")\.\w{2}))$"; 
if (preg_match ( "/" . $matchstr . "/ies", $host, $matchs )) { 
$domain = $matchs ['0']; 
} else { 
$domain = $host; 
} 
return $domain; 
} 


Examples to use:

 
$str = "https://www.ofstack.com/tools/zhengze.html"; 
echo getdomain ( $str ); 

Output: ofstack com

Related articles: