开发者

PHP: Remove 'WWW' from URL inside a String

开发者 https://www.devze.com 2023-03-12 22:38 出处:网络
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?

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?

开发者_Go百科
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;


$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.


preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);

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


$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];
0

精彩评论

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

关注公众号