PHP自訂函數擷取URL中頂層網域的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了PHP自訂函數擷取URL中頂層網域的方法。分享給大家供大家參考,具體如下:

<?php/* * Created on 2016-8-23 */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;}echo top_domain("http://tools.jb51.net/");?>

運行結果為:

更多關於PHP相關內容感興趣的讀者可查看本站專題:《phpRegex用法總結》、《php curl用法總結》、《PHP數組(Array)操作技巧大全》、《php排序演算法總結》、《PHP常用遍曆演算法與技巧總結》、《PHP資料結構與演算法教程》、《php程式設計演算法總結》、《PHP數學運算技巧總結》、《PHP運算與運算子用法總結》、《php字串(string)用法總結》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.