21

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;

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

your solution is good, but it miss the case like www.www.test.com. updated version - preg_replace('#^(?:www\.)+(.+\.)#i', '$1', $domain);
11
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);

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

1 Comment

new version: preg_replace('#^(http(s)?://)?w{3}\.(\w+\.\w+)#', '$1$3', $url); Remove $1 for skipping protocol
6
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.