在没有TLD的情况下获取域名 [英] Getting domain name without TLD

查看:73
本文介绍了在没有TLD的情况下获取域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有此代码:

    // get host name from URL
    preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.joomla.subdomain.php.net/index.html", $matches);
    $host = $matches[1];

    // get last two segments of host name
    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo "domain name is: {$matches[0]}\n";

输出将为php.net

我只需要php而无需.net

推荐答案

将第二个正则表达式的第一部分分组为/([^.]+)\.[^.]+$/,而$matches[1]将是php

Group the first part of your 2nd regex into /([^.]+)\.[^.]+$/ and $matches[1] will be php

这篇关于在没有TLD的情况下获取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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