Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 需要如何从$#u服务器[';HTTP#u主机';]中删除www?_Php - Fatal编程技术网

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.');