开发者

Need to remove www from $_SERVER['HTTP_HOST'] how?

I need to remove www from the st开发者_StackOverflow中文版ring given by $_SERVER['HTTP_HOST']

How can I do that in PHP?


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

That should do the trick...


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


Client requested site name "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"


Instead of str_replace, you could use ltrim:

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


Do you want this:

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


Here

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

You can also

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜