时间:2021-07-01 10:21:17 帮助过:7人阅读
function getdomain($url) {
$host = strtolower ( $url );
if (strpos ( $host, '/' ) !== false) {
$parse = @parse_url ( $host );
$host = $parse ['host'];
}
$topleveldomaindb = array ('com', 'edu', 'gov', 'int', 'mil', 'net', 'org', 'biz', 'info', 'pro', 'name', 'museum', 'coop', 'aero', 'xxx', 'idv', 'mobi', 'cc', 'me' );
$str = '';
foreach ( $topleveldomaindb as $v ) {
$str .= ($str ? '|' : '') . $v;
}
$matchstr = "[^\.]+\.(?:(" . $str . ")|\w{2}|((" . $str . ")\.\w{2}))$";
if (preg_match ( "/" . $matchstr . "/ies", $host, $matchs )) {
$domain = $matchs ['0'];
} else {
$domain = $host;
}
return $domain;
}
使用例子:
代码如下:输出:jb51.net
$str = "http://www.gxlcms.com/tools/zhengze.html";
echo getdomain ( $str );
http://www.bkjia.com/PHPjc/325934.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325934.htmlTechArticlePHP获取网址的顶级域名函数 目前国际顶级域名有:com|edu|gov|int|mil|net|org|biz|info|pro|name|museum|coop|aero|xxx|idv|mobi|cc|me 地域型域名比较多,应该是...