PHP 根据 URL 网址得到域名

function get_domain($url) {
    $comps = parse_url($url);
    $domain = "";
    if(!$comps) {
        echo "not valid url: $url\n";
    } else {
        $hostname = $comps['host'];
        $patt_ip = '/\d+(\.\d+){3}/';
        $num_ip = preg_match($patt_ip, $hostname, $out);
        if ($num_ip) {
            $domain = $hostname;
        } else {
            $patt = '/\.(([a-z0-9A-Z\-]+)(\.(us|com|org|net|info))?(\.cn)?)/';
            $num = preg_match($patt, $hostname, $out);
            if($num) {
                $domain = $out[1];
            }
        }
    }
    return $domain;
}