时间:2021-07-01 10:21:17 帮助过:16人阅读
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;
}
使用例子:
代码如下:输出:bitsCN.com
$str = "http://www.bitsCN.com/tools/zhengze.html";
echo getdomain ( $str );