Ask PHP to help, judge the last character of the host name for the country abbreviation

Source: Internet
Author: User
Ask PHP master to help, to determine the host name last character for the country abbreviation
This post was last edited by kwdpx on 2013-07-05 19:05:08

$host = "www.abc.com";
$splitresult =preg_split ("/\./", $host); Use "." Split host Name
Determine the host name the last character is the country abbreviation, if, the output after three fields, if no, then the output after two
$countsplit =count ($splitresult); Maximum number of prime elements in an array
$pattern 2= '/af|aq|at|au|be|bg|br|ca|ch|cl|cn|de|eg|es|fi|fr|gr|hk|hu|ie|il|in|iq|ir|is|it|jp|kr|mx|nl|no|nz|pe |ph|pr|pt|ru|se|sg|th|tr|tw|uk|us|za/'; Country abbreviation
if (Preg_match ($pattern 2, $splitresult [$countsplit-1])) {
$matchesurl = "". $splitresult [$countsplit-3]. ".". $splitresult [$countsplit-2]. ".". $splitresult [$countsplit-1];
}else{
$matchesurl = "". $splitresult [$countsplit-2]. ".". $splitresult [$countsplit-1];
}
$matchesurl =ltrim ("$matchesurl", ".");


When $host = "www.abc.com", the above code can be correctly obtained abc.com

When $host = "www.abc.com.cn", the above code can also be correctly abc.com.cn

But when $host = "www.abc.cn"; When the above code can't get abc.cn correctly, but the wrong got the www.abc.cn

So you want to include aero|biz|cc|co| in the domain name com|coop|edu|gov|info|int|mil|museum|name|nato| Net|ORG|TV also contains the '/af|aq|at|au|be|bg|br|ca|ch|cl|cn|de|eg|es|fi|fr|gr|hk|hu|ie|il|in|iq|ir|is|it|jp|kr|mx|nl|no|nz |pe|ph|pr|pt|ru|se|sg|th|tr|tw|uk|us|za/'; When the country referred to these, output three fields, such as www.abc.com.cn or www.abc.net.cn output three fields: abc.com.cn, abc.net.cn;

When the domain name does not contain COM or net, and so on, only output two fields, such as: www.abc.cn or www.abc.us output two fields: abc.cn, abc.us;.

Of course the default $host = "Www.abc.com", this is still to output two fields, such as: abc.com

That is, how to add a judgment $pattern2= '/af|aq|at|au|be|bg|br|ca|ch|cl|cn|de|eg|es|fi|fr|gr|hk|hu|ie|il|in|iq|ir|is|it| in the above original code jp|kr|mx|nl|no|nz|pe|ph|pr|pt|ru|se|sg|th|tr|tw|uk|us|za/'; Country abbreviation
Whether it contains
aero|biz|cc|co| com|coop|edu|gov|info|int|mil|museum|name|nato| Net|org|tv

In order to make the above code judgment more accurate, please master help, thank you!!

Share to:


------Solution--------------------
The domain name is the Internet organization management, not how you want to write on how to write
Knowledge about domain name Http://baike.baidu.com/view/43.htm I'm not going to irritate you.

Just as you think, you can write.
$s =<<< TXT
www2.abc.com.cn
www3.abc.com.cn
xx.xx.abc.com.cn
www2.abc.cn
www3.abc.cn
xx.xx.abc.cn
Www2.abc.us
Www3.abc.us
Xx.xx.abc.us
TXT;
$a = Preg_split ("/[\r\n]+/", $s); Domain name to be identified
$g = Explode ('
  • 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.