PHP函数获取URL的子域
PHP中有获取子域名的函数吗?
在下面的示例中,我想获取 URL 的“en”部分:
en.example.com
Is there a function in PHP to get the name of the subdomain?
In the following example I would like to get the "en" part of the URL:
en.example.com
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
这是一个一行解决方案:
或者使用您的示例:
编辑:通过添加双括号修复“仅变量应通过引用传递”。
编辑2:从 PHP 5.4 开始,您可以简单地做:
Here's a one line solution:
Or using your example:
EDIT: Fixed "only variables should be passed by reference" by adding double parenthesis.
EDIT 2: Starting from PHP 5.4 you can simply do:
使用 parse_url 函数。
对于多个子域
Uses the parse_url function.
For multiple subdomains
您可以通过首先获取域名(例如 sub.example.com => example.co.uk)然后使用 strstr 获取子域来完成此操作。
输出:
You can do this by first getting the domain name (e.g. sub.example.com => example.co.uk) and then use strstr to get the subdomains.
Outputs:
http://php.net/parse_url
http://php.net/parse_url
由于域后缀的唯一可靠来源是域注册商,因此您无法在他们不知情的情况下找到子域。
https://publicsuffix.org 上有一个包含所有域名后缀的列表。该网站还链接到 PHP 库: https://github.com/jeremykendall/php-domain-解析器。
请在下面找到一个示例。我还添加了 en.test.co.uk 的示例,这是一个具有多个后缀 (co.uk) 的域。
As the only reliable source for domain suffixes are the domain registrars, you can't find the subdomain without their knowledge.
There is a list with all domain suffixes at https://publicsuffix.org. This site also links to a PHP library: https://github.com/jeremykendall/php-domain-parser.
Please find an example below. I also added the sample for en.test.co.uk which is a domain with a multi suffix (co.uk).
PHP 7.0:使用爆炸函数并创建所有结果的列表。
示例:sub.domain.com
结果: 子
结果: 域
PHP 7.0: Use the explode function and create a list of all the results.
Example: sub.domain.com
Result: sub
Result: domain
简单...
只需阅读$match[1]
工作示例
它与此网址列表完美配合
Simply...
Just read $match[1]
Working example
It works perfectly with this list of urls
最简单、最快的解决方案。
Simplest and fastest solution.
并没有真正的 100% 动态解决方案 - 我也只是试图弄清楚它,并且由于不同的域扩展 (DTL),如果不实际解析所有这些扩展并每次检查它们,这项任务将非常困难:
.com vs .co.uk vs org.uk
最可靠的选择是定义一个常量(或数据库条目等)来存储实际域名,并使用
substr 将其从
$_SERVER['SERVER_NAME']
中删除()现在,如果您在
http://test.mymaindomain.co.uk
下使用此函数,它会给您test
或者如果您有多个子域级别http://another.test.mymaindomain.co.uk
您将获得another.test
- 当然,除非您更新DOMAIN< /代码>。
我希望这有帮助。
There isn't really a 100% dynamic solution - I've just been trying to figure it out as well and due to different domain extensions (DTL) this task would be really difficult without actually parsing all these extensions and checking them each time:
.com vs .co.uk vs org.uk
The most reliable option is to define a constant (or database entry etc.) that stores the actual domain name and remove it from the
$_SERVER['SERVER_NAME']
usingsubstr()
Now if you're using this function under
http://test.mymaindomain.co.uk
it will give youtest
or if you have multiple sub-domain levelshttp://another.test.mymaindomain.co.uk
you'll getanother.test
- unless of course you update theDOMAIN
.I hope this helps.
使用正则表达式、字符串函数、parse_url() 或其组合并不是真正的解决方案。只需使用域
test.en.example.co.uk
测试任何建议的解决方案,不会有任何正确的结果。正确的解决方案是使用使用公共后缀列表解析域的包。我推荐 TLDExtract,这里是示例代码:
Using regex, string functions, parse_url() or their combinations it's not real solution. Just test any of proposed solutions with domain
test.en.example.co.uk
, there will no any correct result.Correct solution is use package that parses domain with Public Suffix List. I recomend TLDExtract, here is sample code:
我发现最好和最短的解决方案是
What I found the best and short solution is
对于那些收到“错误:严格标准:只有变量应该通过引用传递”的人。
像这样使用:
$env = (explode(".",$_SERVER['HTTP_HOST']));
$env = array_shift($env);
For those who get 'Error: Strict Standards: Only variables should be passed by reference.'
Use like this:
$env = (explode(".",$_SERVER['HTTP_HOST']));
$env = array_shift($env);
正如上一个问题中所见:
如何使用 PHP 获取第一个子域?
As seen in a previous question:
How to get the first subdomain with PHP?
只需
reset(explode(".", $_SERVER['HTTP_HOST']))
Simply
reset(explode(".", $_SERVER['HTTP_HOST']))
我正在做这样的事情
I'm doing something like this
假设当前 url = sub.example.com
Suppose current url = sub.example.com
这是我的解决方案,它适用于最常见的域,您可以根据需要安装扩展数组:
this is my solution, it works with the most common domains, you can fit the array of extensions as you need:
我知道我真的迟到了,但还是这样吧。
我所做的是获取 HTTP_HOST 服务器变量 (
$_SERVER['HTTP_HOST']
) 和域中的字母数(因此对于example.com
来说,它将是 11 )。然后我使用
substr
函数来获取子域。我确实在 12 而不是 11 处切断了子字符串,因为第二个参数的子字符串从 1 开始。因此,现在如果您输入 test.example.com,则
$subdomain
的值将为test
。这比使用
explode
更好,因为如果子域中有.
,这不会将其切断。I know I'm really late to the game, but here goes.
What I did was take the HTTP_HOST server variable (
$_SERVER['HTTP_HOST']
) and the number of letters in the domain (so forexample.com
it would be 11).Then I used the
substr
function to get the subdomain. I didI cut the substring off at 12 instead of 11 because substrings start on 1 for the second parameter. So now if you entered test.example.com, the value of
$subdomain
would betest
.This is better than using
explode
because if the subdomain has a.
in it, this will not cut it off.如果您使用 drupal 7
这将帮助您:
if you are using drupal 7
this will help you:
从 PHP 5.3 开始,您可以将 strstr() 与 true 参数一起使用
From PHP 5.3 you can use strstr() with true parameter
试试这个...
Try this...
你也可以用这个
you can use this too
也许我迟到了,但尽管这篇文章很旧,但正如我所看到的那样,许多其他人也这样做了。
今天,轮子已经发明了,有一个名为 php-domain-parser 的库是活跃的,并且可以使用两种机制。
一种基于公共后缀列表,另一种基于IANA 列表。
简单而有效,它使我们能够创建简单的助手来帮助我们完成项目,并且能够在扩展及其变体非常多变的世界中知道数据是否得到维护。
本文中给出的许多答案都没有通过一系列单元测试,其中检查了某些当前扩展及其具有多个级别的变体,也没有通过具有扩展字符的域的诡辩。
也许它为你服务,就像它为我服务一样。
Maybe I'm late, but even though the post is old, just as I get to it, many others do.
Today, the wheel is already invented, with a library called php-domain-parser that is active, and in which two mechanisms can be used.
One based on the Public Suffix List and one based on the IANA list.
Simple and effective, it allows us to create simple helpers that help us in our project, with the ability to know that the data is maintained, in a world in which the extensions and their variants are very changeable.
Many of the answers given in this post do not pass a battery of unit tests, in which certain current extensions and their variants with multiple levels are checked, and neither with the casuistry of domains with extended characters.
Maybe it serves you, as it served me.
如果您只想要第一个周期之前的内容:
If you only want what comes before the first period: