如果找到子域,则从URL字符串中删除子域 [英] Delete subdomain from url string if subdomain is found

查看:72
本文介绍了如果找到子域,则从URL字符串中删除子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列这样的域:

domain.com
second.com
www.third.com
www.fourth.fifth.com
sixth.com
seventh.eigth.com

我想要的是一个仅向我返回主机的函数.没有子域.

what I want is a function to return me the host only. Without subdomain.

到目前为止,此代码是我用来获取主机名的代码:

This code is what i have so far for getting the hostname:

$parse = parse_url($url);
$domain = $parse['host'];

但这只会返回此值:

domain.com
second.com
third.com
fourth.fifth.com
sixth.com
seventh.eigth.com

尽管如此,我将需要此输出:

I would need this output though:

domain.com
second.com
third.com
fifth.com
sixth.com
eigth.com

推荐答案

function giveHost($host_with_subdomain) {
    $array = explode(".", $host_with_subdomain);

    return (array_key_exists(count($array) - 2, $array) ? $array[count($array) - 2] : "").".".$array[count($array) - 1];
}

这篇关于如果找到子域,则从URL字符串中删除子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆