PHP: Remove 'WWW' from URL inside a String

ritch picture ritch · Jun 13, 2011 · Viewed 20.1k times · Source

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;

Answer

Floern picture Floern · Jun 13, 2011
$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.