Php 需要如何从$#u服务器[';HTTP#u主机';]中删除www?
我需要从Php 需要如何从$#u服务器[';HTTP#u主机';]中删除www?,php,Php,我需要从$\u SERVER['HTTP\u HOST'] 在PHP中如何实现这一点?这里 $host = str_replace('www.', null, $_SERVER['HTTP_HOST']) 你也可以 $ht = "http://"; $host = str_replace('www.',$ht, $_SERVER['HTTP_HOST']) 这样就可以了…您想要这个吗: if (substr($_SERVER['HTTP_HOST'], 0, 4) == 'www.')
$\u SERVER['HTTP\u HOST']
在PHP中如何实现这一点?这里
$host = str_replace('www.', null, $_SERVER['HTTP_HOST'])
你也可以
$ht = "http://";
$host = str_replace('www.',$ht, $_SERVER['HTTP_HOST'])
这样就可以了…您想要这个吗:
if (substr($_SERVER['HTTP_HOST'], 0, 4) == 'www.') {
$domain = substr($_SERVER['HTTP_HOST'], 4);
} else {
$domain = $_SERVER['HTTP_HOST'];
}
$_SERVER['HTTP_HOST'] = str_replace('www.', '',$_SERVER['HTTP_HOST']);
客户请求的站点名称“www.example.com”
您可以使用
ltrim
,而不是str\u replace
:
$sender_domain = $_SERVER[HTTP_HOST];
$sender_domain = ltrim($sender_domain, 'www.');
这将从主机中的任何位置删除“www”,而不仅仅是在开始时。如果某人的域名是“my-www.com”,他们会得到意想不到的结果。
// explode the string at the '.' and inserts into an array
$pieces = explode(".", $_SERVER['HTTP_HOST']);
// $pieces array is now:
// now $pieces[0] contains "www"
// now $pieces[1] contains "example"
// now $pieces[2] contains "com"
// Put the domain name back together using concatenation operator ('.')
$DomainNameWithoutHostPortion = $pieces[1].'.'.$pieces[2];
// now $DomainNameWithoutHostPortion contains "example.com"
$sender_domain = $_SERVER[HTTP_HOST];
$sender_domain = ltrim($sender_domain, 'www.');