Is it possible to get a first-level domain name code for each type of URL?

Source: Internet
Author: User
There are many URLs, like abc.abc.com, Abc.com/abc, www.abc.com.cn, abc.com.tw, www.abc.co.uk, WWW.ABC.COM.JP/ABC.PHP/ID=ABC (id= It can be long after ABC)
There are a lot of things that I hope can be omnipotent.

How do I get the first-level domain name for all URLs in PHP? Get results for abc.com,abc.com.cn, abc.co.uk?

The situation is more complicated. I use the following code, roughly can be judged, but when the domain name has Com,net,org,gov,cc,biz,info,cn,co, the result is a problem.
Www.cool.com, for example, became www.co.
Hope that the regular master to help me to modify, become a universal domain name code.

$url = $row ["url"];p reg_match ("#[\w-]+\. Com|net|org|gov|cc|biz|info|cn|co) (\. ( Cn|hk|uk) *# ", $url, $match); Echo $match [0];



Reply to discussion (solution)

$s = <<< textabc.abc.comabc.com/abcwww.abc.com.cnabc.com.twwww.abc.co.ukwww.abc.com.jp/abc.php/id= Abcwww.cool.comtext;foreach (Split ("[\r\n]+", $s) as $url) {  preg_match ("#[\w-]+\. Com|net|org|gov|cc|biz|info|cn|co) \b (\. ( CN|HK|UK|JP|TW)) *# ", $url, $match);  echo "


" . $match [0];}


Abc.abc.com
ABC.com

Abc.com/abc
ABC.com

www.abc.com.cn
abc.com.cn

abc.com.tw
abc.com.tw

www.abc.co.uk
abc.co.uk

Www.abc.com.jp/abc.php/id=abc
abc.com.jp

Www.cool.com
Cool.com

Upstairs positive solution

The code is omnipotent, the moderator is also omnipotent.
Cheers ~ ~

www.net.cn domain name, this parsing out is not right, similar to the www.com.cn, of course, is a very special example

  • 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.