我想从网址获取主域名。我的网址:
http://cs541402.vk.me/u170785079/video/l_051000aa.jpg
我想回复: vk.me 我尝试了很多方法,但没有为我工作
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
$url = 'http://cs541402.vk.me/u170785079/video/l_051000aa.jpg';
$url = 'http://' . get_domain($url);
print get_domain($url);
答案 0 :(得分:0)
$url = 'http://vk.me/u170785079/video/l_051000aa.jpg';
$info = parse_url($url);
$host = $info['host'];
$host_names = explode(".", $host);
$bottom_host_name = $host_names[count($host_names)-2] . "." . $host_names[count($host_names)-1];
echo $bottom_host_name;
答案 1 :(得分:0)
$url = 'http://cs541402.vk.me/u170785079/video/l_051000aa.jpg';
$info = parse_url($url);
$host = $info['host'];
// get only the host and explode at '.'
$host_names = explode(".", $host);
// with the array of segments from the exploded hostname, get the last 2 elements
$host_names = array_slice($host_names, -2, 2, true);
// implode our returned array back together, or access them directly
echo implode('.',$host_names);