PHP functions for extracting first-class domain names

Source: Internet
Author: User
The regular_domain function of PHP to extract top-level domain names is used to convert the domain name into a standard & #26684; format. top_domain is used to extract top-level domain names. The test results are good. Functionregular_domain ($ domain) {if (substr ($ domain,) {$ domainsubstr ($ domain, 7);} if PHP extracts top-level domain name functions

Regular_domain is used to convert a domain name to a standard format, and top_domain is used to extract top-level domain names. The test results are good.


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 strtoupper ($ domain );

}


Function top_domain ($ domain)
{
$ Domain = regular_domain ($ domain );
$ Iana_root = array ('AC', 'AD', 'AE', 'Aero', 'AF', 'AG', 'Ai', 'Al ', 'AM', 'Any', 'ao', 'Aq ', 'ar', 'arpa', 'as', 'as', '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 ', 'D', 'DJ', 'dk ', 'DM', 'Do ', 'dz', 'EC', 'edu', 'ee ', 'EG', 'eh ', 'ER', 'es ', 'ET', 'EU ', 'Fi', 'fj ', 'fk', 'FM ', 'Fo', 'Fr ', 'ga', 'G ', 'gd ', 'GE', 'GF', 'GG ', 'GH', 'GI', 'Gl', 'G', '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 ', 'Lil', 'LK', 'LR ', 'Ls', 'Lt ', 'Lu ', 'LV', 'ly ', 'M', 'MC', 'MD', 'me', 'MF ', 'MG', 'MH ', 'mil ', 'MK', 'ml', 'mm', 'mn ', 'mobi', 'MP', 'mq ', 'Mr ', 'MS', 'mt', 'Mu ', 'Museum', 'mmv ', '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', 'ps', 'pn ', 'PR', 'pro', 'ps', 'pt', 'pw', 'py ', 'qa ', 're', 'Ro', 'Rs ', 'Ru', 'rw ', 'sa', 'sb ', 'scs', 'SD ', 'se', 'SG ', 'sh', 'Si', 'S1', 'SK', 'S1', 'SS', 'SS ', 'sr', 'SS', 'st', 'su', 'sv', 'SS', 'sys', 'sz ', 'TC', 'TD ', 'tel ', 'tf', 'tg', 'th', 'TJ ', 'TK', 'Tl ', 'TT', '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', 'z', '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;
}

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.