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