Ask a php expert to help determine whether the last character of the host name is the country abbreviation.

Source: Internet
Author: User
Ask a php expert to help determine whether the last character of the host name is "National abbreviation". the Last Post is edited by kwdpx from 2013-07-0519: 05: 08 $ host & nbsp; www.abc.com; $ splitresultpreg_split (., $ host); & nbsp. split the host name to determine whether the last character of the host name is the country.

This post was last edited by kwdpx at 19:05:08 on

$ Host = "www.abc.com ";
$ Splitresult = preg_split ("/\./", $ host); // Use "." to separate host names
// Determine whether the last character of the host name is the country abbreviation. If yes, the last three fields are Output. If no, the last two fields are output.
$ Countsplit = count ($ splitresult); // The largest element prime number in the array
$ 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 | jp | kr | mx | nl | no | nz | pe | ph | pr | pt | ru | se | sg | th | tr | tw | uk | us | za /'; // country abbreviation
If (preg_match ($ pattern2, $ 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 is correct, you can get abc.com

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

However, when $ host = "www.abc.cn";, the above code does not get abc.cn correctly, but does get www.abc.cn by mistake.

Therefore, if the domain name contains aero | biz | cc | co | com | coop | edu | gov | info | int | mil | museum | name | nato | net | org | TV it also contains '/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 is short for these, three fields are output, for example, www.abc.com.cn or www.abc.net.cn: abc.com.cn and abc.net.cn;

When the domain name does not contain com or net, only two fields are output, for example, www.abc.cn or www. abc. us. The two fields are output: abc.cn and abc. us ;.

Of course, the default value is $ host = "www.abc.com". In this case, two fields must be output, for example, abc.com.

That is, how to use the original code above, add a criterion $ 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 | jp | kr | mx | nl | no | nz | pe | ph | pr | pt | ru | se | sg | th | tr | tw | uk | us | za /'; // country abbreviation
Include or not
Aero | biz | cc | co | com | coop | edu | gov | info | int | mil | museum | name | nato | net | org | TV

To make the above code more accurate, please help, thank you !!

Share:


------ Solution --------------------
Domain names are managed by Internet organizations. they are not written in any way.
Knowledge of domain names visible http://baike.baidu.com/view/43.htm I will not stimulate you

You can write as you want.
$ 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 recognized
$ G = explode ('

Related Article

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.