PHP:从字符串内的URL中删除"WWW"
问题描述:
当前,我正在使用parse_url,但是数组的主机项还包含了我不想要的'WWW'部分.我将如何删除它?
Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;
答
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];
这不会删除www.com
中的www
,但是www.www.com
会导致www.com
.
This won't remove the www
in www.com
, but www.www.com
results in www.com
.