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

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

问题描述

我有一个这样的域数组:

I have an array of domains like this:

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

不过我需要这个输出:

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天全站免登陆