需要从$ _SERVER ['HTTP_HOST']中删除www怎么样?

时间:2011-08-02 23:25:55

标签: php

我需要从$_SERVER['HTTP_HOST']

指定的字符串中删除www

我怎么能用PHP做到这一点?

6 个答案:

答案 0 :(得分:13)

$server_name = str_replace("www.", "", $_SERVER['HTTP_HOST']);

应该这样做......

答案 1 :(得分:6)

if (substr($_SERVER['HTTP_HOST'], 0, 4) == 'www.') {
    $domain = substr($_SERVER['HTTP_HOST'], 4);
} else {
    $domain = $_SERVER['HTTP_HOST'];
}

答案 2 :(得分:1)

客户要求的网站名称" www.example.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"

答案 3 :(得分:0)

你想要这个:

$_SERVER['HTTP_HOST'] = str_replace('www.', '',$_SERVER['HTTP_HOST']);

答案 4 :(得分:0)

而不是str_replace,您可以使用ltrim

$sender_domain = $_SERVER[HTTP_HOST];
$sender_domain = ltrim($sender_domain, 'www.');

答案 5 :(得分:-1)

下面

$host = str_replace('www.', null, $_SERVER['HTTP_HOST'])

你也可以

 $ht = "http://";
 $host = str_replace('www.',$ht, $_SERVER['HTTP_HOST'])