ritch
ritch

Reputation: 1808

PHP: Remove 'WWW' from URL inside a String

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;

Upvotes: 21

Views: 23754

Answers (3)

Empty
Empty

Reputation: 457

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

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

Upvotes: 11

Floern
Floern

Reputation: 33904

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

Upvotes: 35

Frank Farmer
Frank Farmer

Reputation: 39356

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

Upvotes: 6

Related Questions