PHP: Remove 'WWW' from URL inside a String

2019-01-24 04:11发布

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;

4条回答
干净又极端
2楼-- · 2019-01-24 04:57
str_ireplace('www.','',$parse['host']); 
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-24 04:58
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];

This won't remove the www in www.com, but www.www.com results in www.com.

查看更多
该账号已被封号
4楼-- · 2019-01-24 04:59
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];
查看更多
姐就是有狂的资本
5楼-- · 2019-01-24 05:03
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);

if you don't need a protocol prefix, leave the second parameter empty

查看更多
登录 后发表回答