PHP函数获取URL的子域 [英] PHP function to get the subdomain of a URL

查看:56
本文介绍了PHP函数获取URL的子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中是否有一个函数来获取子域的名称?

Is there a function in PHP to get the name of the subdomain?

在下面的示例中,我想获取URL的"en"部分:

In the following example I would like to get the "en" part of the URL:

en.example.com

推荐答案

这里是一种解决方案:

array_shift((explode('.', $_SERVER['HTTP_HOST'])));

或使用您的示例:

array_shift((explode('.', 'en.example.com')));

通过添加双括号修复了仅变量应通过引用传递".


EDIT 2 :从 PHP 5.4 开始可以简单地做到:


EDIT 2: Starting from PHP 5.4 you can simply do:

explode('.', 'en.example.com')[0];

这篇关于PHP函数获取URL的子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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