1

I have the following 3 urls

http://www.yahoo.co.uk and http://yahoo.co.uk, the difference being that the later doesn't have www in it. When using parse_url on both these url's, the output is the following.

{"scheme":"http","host":"www.yahoo.co.uk"}

{"scheme":"http","host":"yahoo.co.uk"}

Both url's being the same, comparing them using a == or using strpos or explode the text with . and compare each array doesn't seem to be ideal.

I have looked at the other questions on Stack Overflow that discusses about comparing URL's but they don't seem to be addressing the issue I have mentioned above. Appreciate if someone could help me figure out how to solve this issue.

3
  • does it matter if there's anything after the domain? such as /index/?module=blah? Commented Sep 29, 2013 at 4:52
  • 2
    The URLs are not the same. There's no rule that says that www.DOMAIN has to be the same as DOMAIN, although it usually is. Commented Sep 29, 2013 at 4:56
  • Actually, there are 2 different domains. Try to cut off the "www" part and then compare what you have to compare there. Commented Sep 29, 2013 at 5:00

1 Answer 1

3

Simple helper functions:

function compareUrls($a, $b) {
    $a = parse_url($a, PHP_URL_HOST);
    $b = parse_url($b, PHP_URL_HOST);

    return trimWord($a) === trimWord($b);
}

function trimWord($str) {
    if (stripos($str, 'www.') === 0) {
        return substr($str, 4);
    }
    return $str;
}

var_dump(compareUrls('http://www.yahoo.co.uk', 'http://yahoo.co.uk'));

Outputs:

bool(true)
Sign up to request clarification or add additional context in comments.

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.