求php好手來幫忙,判斷主機名稱最後一個字元為國家簡稱

來源:互聯網
上載者:User
求php高手來幫忙,判斷主機名稱最後一個字元為國家簡稱
本帖最後由 kwdpx 於 2013-07-05 19:05:08 編輯

$host = "www.abc.com";
$splitresult=preg_split("/\./",$host); //用"."分割主機名稱
//判斷主機名稱最後一個字元為國家簡稱,若是,則輸出後三個欄位,若否,則輸出後兩個
$countsplit=count($splitresult); //數組中最大元素數
$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/'; //國家簡稱
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", ".");


當$host = "www.abc.com";以上代碼可以正確得到abc.com

當$host = "www.abc.com.cn";以上代碼也能正確得到abc.com.cn

但當$host = "www.abc.cn"; 時,以上代碼不能正確得到abc.cn,而是錯誤的得到了www.abc.cn

所以想當網域名稱中包含aero|biz|cc|co|com|coop|edu|gov|info|int|mil|museum|name|nato|net|org|tv的同時又包含'/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/'; //國家簡稱這些的時候,輸出三個欄位,如www.abc.com.cn或www.abc.net.cn輸出三個欄位:abc.com.cn、abc.net.cn;

當網域名稱中不包含com或net等時,只輸出二個欄位,如:www.abc.cn或www.abc.us則輸出二個欄位:abc.cn、abc.us;。

當然預設$host = "www.abc.com";這種還是要輸出二個欄位的,如:abc.com

也就是如何在以上原有代碼中,加一個判斷$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/'; //國家簡稱
是否包含
aero|biz|cc|co|com|coop|edu|gov|info|int|mil|museum|name|nato|net|org|tv

以使以上代碼判斷更精準,請高手幫忙,謝!!

分享到:


------解決方案--------------------
網域名稱是國際互連網組織管理的,不是你想怎麼寫就怎麼寫的
有關網域名稱的知識可見 http://baike.baidu.com/view/43.htm 我就不刺激你了

就按你的想法,可以寫作
$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); //待識別網域名稱
$g = explode('
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.