PHP Custom Function Gets the first-level domain name in the URL _php tips

Source: Internet
Author: User
Tags php programming php regular expression

This article illustrates how PHP custom functions get the first-level domain name in a URL. Share to everyone for your reference, specific as follows:

<?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);
  The function Top_domain ($domain) {$domain = Regular_domain ($domain);
      $iana _root = Array (' AC ', ' ad ', ' AE ', ' aero ', ' AF ', ' AG ', ' ai ', ' al ',
      ' AM ', ' a ', ' 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 ', ' I O ', ' IQ ', ' ir ', ' is ', ' it ', ' je ', ' jm ', ' Jo ', ' Jobs ', ' JP ', ' K E ', ' 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 ', ' NE T ', ' 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.c
    OM 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/");?>

The results of the operation are:

More about PHP Interested readers can view the site topics: "PHP Regular Expression Usage summary", "Php Curl Usage Summary", "PHP Array" operation Skills Encyclopedia, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Mathematical Calculation Skills Summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.