PHP从子域名获取域名 [英] PHP Getting Domain Name From Subdomain
问题描述
我需要编写一个函数来解析包含域名的变量。最好我用一个例子来解释一下,变量可以包含以下任何一种:
I need to write a function to parse variables which contain domain names. It's best I explain this with an example, the variable could contain any of these things:
here.example.com
example.com
example.org
here.example.org
但是当通过我的函数时,所有这些都必须返回example.com或example.co.uk,根域名基本上。我相信我以前做过,但我一直在搜索Google约20分钟,找不到任何东西。任何帮助将不胜感激。
But when passed through my function all of these must return either example.com or example.co.uk, the root domain name basically. I'm sure I've done this before but I've been searching Google for about 20 minutes and can't find anything. Any help would be appreciated.
编辑:忽略.co.uk,假设所有通过此功能的域都有3个字母的TLD。
Ignore the .co.uk, presume that all domains going through this function have a 3 letter TLD.
推荐答案
Stackoverflow问题归档:
- 如何从网址获取域名?
- 检查域是否等于值?
- 我如何获得基本网址?
- How to get domain name from url?
- Check if domain equals value?
- How do I get the base url?
Stackoverflow Question Archive:
print get_domain("http://somedomain.co.uk"); // outputs 'somedomain.co.uk'
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;
}
这篇关于PHP从子域名获取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!