PHP 获取url的一级域名的方法

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:57   1453   0
<?php
function regular_domain($domain) 

{
 if (substr ( $domain, 0, 7 ) == 'http://') {
  $domain = substr ( $domain, 7 );
 }
 if (strpos ( $domain, '/' ) !== false) {
  $domain = substr ( $domain, 0, strpos ( $domain, '/' ) );
 }
 return strtolower ( $domain );
}
function top_domain($domain) {
 $domain = regular_domain ( $domain );
 $iana_root = array (
   'ac',
   'ad',
   'ae',
   'aero',
   'af',
   'ag',
   'ai',
   'al',
   'am',
   'an',
   'ao',
   'aq',
   'ar',
   'arpa',
   'as',
   'asia',
   'at',
   'au',
   'aw',
   'ax',
   'az',
   'ba',
   'bb',
   'bd',
   'be',
   'bf',
   'bg',
   'bh',
   'bi',
   'biz',
   'bj',
   'bl',
   'bm',
   'bn',
   'bo',
   'bq',
   'br',
   'bs',
   'bt',
   'bv',
   'bw',
   'by',
   'bz',
   'ca',
   'cat',
   'cc',
   'cd',
   'cf',
   'cg',
   'ch',
   'ci',
   'ck',
   'cl',
   'cm',
   'cn',
   'co',
   'com',
   'coop',
   'cr',
   'cu',
   'cv',
   'cw',
   'cx',
   'cy',
   'cz',
   'de',
   'dj',
   'dk',
   'dm',
   'do',
   'dz',
   'ec',
   'edu',
   'ee',
   'eg',
   'eh',
   'er',
   'es',
   'et',
   'eu',
   'fi',
   'fj',
   'fk',
   'fm',
   'fo',
   'fr',
   'ga',
   'gb',
   'gd',
   'ge',
   'gf',
   'gg',
   'gh',
   'gi',
   'gl',
   'gm',
   'gn',
   'gov',
   'gp',
   'gq',
   'gr',
   'gs',
   'gt',
   'gu',
   'gw',
   'gy',
   'hk',
   'hm',
   'hn',
   'hr',
   'ht',
   'hu',
   'id',
   'ie',
   'il',
   'im',
   'in',
   'info',
   'int',
   'io',
   'iq',
   'ir',
   'is',
   'it',
   'je',
   'jm',
   'jo',
   'jobs',
   'jp',
   'ke',
   'kg',
   'kh',
   'ki',
   'km',
   'kn',
   'kp',
   'kr',
   'kw',
   'ky',
   'kz',
   'la',
   'lb',
   'lc',
   'li',
   'lk',
   'lr',
   'ls',
   'lt',
   'lu',
   'lv',
   'ly',
   'ma',
   'mc',
   'md',
   'me',
   'mf',
   'mg',
   'mh',
   'mil',
   'mk',
   'ml',
   'mm',
   'mn',
   'mo',
   'mobi',
   'mp',
   'mq',
   'mr',
   'ms',
   'mt',
   'mu',
   'museum',
   'mv',
   'mw',
   'mx',
   'my',
   'mz',
   'na',
   'name',
   'nc',
   'ne',
   'net',
   'nf',
   'ng',
   'ni',
   'nl',
   'no',
   'np',
   'nr',
   'nu',
   'nz',
   'om',
   'org',
   'pa',
   'pe',
   'pf',
   'pg',
   'ph',
   'pk',
   'pl',
   'pm',
   'pn',
   'pr',
   'pro',
   'ps',
   'pt',
   'pw',
   'py',
   'qa',
   're',
   'ro',
   'rs',
   'ru',
   'rw',
   'sa',
   'sb',
   'sc',
   'sd',
   'se',
   'sg',
   'sh',
   'si',
   'sj',
   'sk',
   'sl',
   'sm',
   'sn',
   'so',
   'sr',
   'ss',
   'st',
   'su',
   'sv',
   'sx',
   'sy',
   'sz',
   'tc',
   'td',
   'tel',
   'tf',
   'tg',
   'th',
   'tj',
   'tk',
   'tl',
   'tm',
   'tn',
   'to',
   'tp',
   'tr',
   'travel',
   'tt',
   'tv',
   'tw',
   'tz',
   'ua',
   'ug',
   'uk',
   'um',
   'us',
   'uy',
   'uz',
   'va',
   'vc',
   've',
   'vg',
   'vi',
   'vn',
   'vu',
   'wf',
   'ws',
   'xxx',
   'ye',
   'yt',
   'za',
   'zm',
   'zw' 
 );
 $sub_domain = explode ( '.', $domain );
 $top_domain = '';
 $top_domain_count = 0;
 for($i = count ( $sub_domain ) - 1; $i >= 0; $i --) {
  if ($i == 0) {
   // just in case of something like NAME.COM
   break;
  }
  if (in_array ( $sub_domain [$i], $iana_root )) {
   $top_domain_count ++;
   $top_domain = '.' . $sub_domain [$i] . $top_domain;
   if ($top_domain_count >= 2) {
    break;
   }
  }
 }
 $top_domain = $sub_domain [count ( $sub_domain ) - $top_domain_count - 1] . $top_domain;
 return $top_domain;
}
?>

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP