PHP中是否有一个函数来获取子域的名称?
在下面的示例中,我想获取URL的“ en”部分:
en.example.com
这里是单行解决方案:
array_shift((explode('.', $_SERVER['HTTP_HOST'])));
或使用您的示例:
array_shift((explode('.', 'en.example.com')));
编辑:通过添加双括号来修复“仅变量应按引用传递”。
编辑2:从PHP 5.4开始,您可以简单地执行以下操作:
explode('.', 'en.example.com')[0];
对于那些获得“错误:严格标准:只有变量应该通过引用传递的人。”像这样使用:
$domain = 'sub.dev.example.com';
$tmp = explode('.', $domain); // split into parts
$subdomain = current($tmp);
print($subdomain); // prints "sub"
并没有真正的100%动态解决方案-我也一直在尝试解决这个问题,由于域扩展名(DTL)不同,如果不对所有这些扩展名进行实际分析并检查每个扩展名,这项任务将非常困难。时间:
简单
使用正则表达式,字符串函数,parse_url()或它们的组合不是真正的解决方案。只需使用域test.en.example.co.uk
测试任何建议的解决方案,就不会有任何正确的结果。
PHP 7.0:使用爆炸功能并创建所有结果的列表。
这是我的解决方案,它适用于最常见的域,您可以根据需要容纳一系列扩展:
// For www.abc.en.example.com
$host_Array = explode(".",$_SERVER['HTTP_HOST']); // Get HOST as array www, abc, en, example, com
array_pop($host_Array); array_pop($host_Array); // Remove com and exmaple
array_shift($host_Array); // Remove www (Optional)
echo implode($host_Array, "."); // Combine array abc.en
我知道我真的很迟到,但是可以。
如果您使用的是drupal 7
使用parse_url功能。
$url = 'http://en.example.com';
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
$subdomain = $host[0];
echo $subdomain;
对于多个子域
$url = 'http://usa.en.example.com';
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
$subdomains = array_slice($host, 0, count($host) - 2 );
print_r($subdomains);
$host = $_SERVER['HTTP_HOST'];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
$domain = $matches[0];
$url = explode($domain, $host);
$subdomain = str_replace('.', '', $url[0]);
echo 'subdomain: '.$subdomain.'<br />';
echo 'domain: '.$domain.'<br />';
从PHP 5.3开始,您可以将strstr()
尝试一下...
function get_subdomain($url=""){
if($url==""){
$url = $_SERVER['HTTP_HOST'];
}
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['path']);
$subdomains = array_slice($host, 0, count($host) - 2 );
return implode(".", $subdomains);
}
您也可以使用它
我正在做这样的事情
我们使用此功能来处理多个子域
假设当前网址= sub.example.com
如果您只想要第一期之前的内容:
您可以先获取域名(例如sub.example.com => example.co.uk),然后使用strstr来获取子域。
$testArray = array(
'sub1.sub2.example.co.uk',
'sub1.example.com',
'example.com',
'sub1.sub2.sub3.example.co.uk',
'sub1.sub2.sub3.example.com',
'sub1.sub2.example.com'
);
foreach($testArray as $k => $v)
{
echo $k." => ".extract_subdomains($v)."\n";
}
function extract_domain($domain)
{
if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches))
{
return $matches['domain'];
} else {
return $domain;
}
}
function extract_subdomains($domain)
{
$subdomains = $domain;
$domain = extract_domain($subdomains);
$subdomains = rtrim(strstr($subdomains, $domain, true), '.');
return $subdomains;
}
输出:
0 => sub1.sub2
1 => sub1
2 =>
3 => sub1.sub2.sub3
4 => sub1.sub2.sub3
5 => sub1.sub2
<?php
$url = 'http://user:[email protected]/path?argument=value#anchor';
$array=parse_url($url);
$array['host']=explode('.', $array['host']);
echo $array['host'][0]; // returns 'en'
?>
由于域名注册机构是域名后缀的唯一可靠来源,因此在没有子域名的情况下您无法找到子域。在https://publicsuffix.org处有所有域后缀的列表。该站点还链接到PHP库:https://github.com/jeremykendall/php-domain-parser。
请在下面找到示例。我还添加了en.test.co.uk的示例,该示例是一个具有多个后缀(co.uk)的域。
<?php
require_once 'vendor/autoload.php';
$pslManager = new Pdp\PublicSuffixListManager();
$parser = new Pdp\Parser($pslManager->getList());
$host = 'http://en.example.com';
$url = $parser->parseUrl($host);
echo $url->host->subdomain;
$host = 'http://en.test.co.uk';
$url = $parser->parseUrl($host);
echo $url->host->subdomain;
preg_match('/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i', $url, $match);
只需阅读$ match [1]
与此URL列表完美配合使用
$url = array(
'http://www.domain.com', // www
'http://domain.com', // --nothing--
'https://domain.com', // --nothing--
'www.domain.com', // www
'domain.com', // --nothing--
'www.domain.com/some/path', // www
'http://sub.domain.com/domain.com', // sub
'опубликованному.значения.ua', // опубликованному ;)
'значения.ua', // --nothing--
'http://sub-domain.domain.net/domain.net', // sub-domain
'sub-domain.third-Level_DomaIN.domain.uk.co/domain.net' // sub-domain
);
foreach ($url as $u) {
preg_match('/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/i', $u, $match);
var_dump($match);
}
最简单,最快的解决方案。
$sSubDomain = str_replace('.example.com','',$_SERVER['HTTP_HOST']);
$REFERRER = $_SERVER['HTTP_REFERER']; // Or other method to get a URL for decomposition
$domain = substr($REFERRER, strpos($REFERRER, '://')+3);
$domain = substr($domain, 0, strpos($domain, '/'));
// This line will return 'en' of 'en.example.com'
$subdomain = substr($domain, 0, strpos($domain, '.'));
我发现最好的解决方案是]
array_shift(explode(".",$_SERVER['HTTP_HOST']));