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;
}
php/domain-name.txt · 最后更改: 2010/06/02 09:18 (外部编辑)
2007~2011 Copyright @ http://www.pythonclub.org