Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings (urls) for same domain

Tags:

url

php

compare

I'm trying to compare two urls using PHP, ensuring that the domain name is the same. It cannot be the sub-domain. It has to literally be the same domain. Example:

http://www.google.co.uk would validate as true compared to http://www.google.co.uk/pages.html.

but

http://www.google.co.uk would validate as false compared to http://www.something.co.uk/pages.html.

like image 301
user398341 Avatar asked Sep 18 '11 09:09

user398341


4 Answers

Use parse_url(), and compare the "host" index in the array returned from the two calls to parse_url().

like image 114
Ariel Avatar answered Sep 23 '22 13:09

Ariel


Use parse_url()

$url1 = parse_url("http://www.google.co.uk");
$url2 = parse_url("http://www.google.co.uk/pages.html");

if ($url1['host'] == $url2['host']){
   //matches
}
like image 23
genesis Avatar answered Sep 23 '22 13:09

genesis


simple, use parse_url()

$url1 = parse_url('http://www.google.co.uk');
$url2 = parse_url('http://www.google.co.uk/pages.html');

if($url1['host'] == $url2['host']){
    // same domain
}
like image 44
Mihai Iorga Avatar answered Sep 20 '22 13:09

Mihai Iorga


You could use parse_url for this

$url1 = parse_url('http://www.google.com/page1.html');
$domain1 = $url1['host'];

$url2 = parse_url('http://www.google.com/page2.html');
$domain2 = $url2['host'];

if($domain1 == $domain2){
// something
}
like image 23
robjmills Avatar answered Sep 24 '22 13:09

robjmills