Compare two strings (urls) for same domain

phpuriurldocumentationcompare

Last Update : 2023-09-22 UTC 10:05:16 AM

Answers of > Compare two strings (urls) for same domain

w3coded php compare Stack Overflow for Teams w3coded php compare Where developers & technologists w3coded php compare share private knowledge with coworkers w3coded php compare ,I'm trying w3coded php compare to compare two urls using PHP, ensuring that the w3coded php compare domain name is the same. It cannot be the w3coded php compare sub-domain. It has to literally be the same w3coded php compare domain. Example:,Thanks for contributing an w3coded php compare answer to Stack Overflow!, w3coded php compare w3coded php compare Meta Stack w3coded php compare Overflow

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
}

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
}

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
}

Expanding the answer given by Ariel, the code you could use is similar to the following one:

<?php

compare_host('http://www.google.co.uk', 'http://www.something.co.uk/pages.html');

function compare_host($url1, $url2)
{
    // PHP prior of 5.3.3 emits a warning if the URL parsing failed.
    $info = @parse_url($url1);
    if (empty($info)) {
        return FALSE;
    }
    $host1 = $info['host'];
    $info = @parse_url($url2);
    if (empty($info)) {
        return FALSE;
    }
    return (strtolower($host1) === strtolower($info['host']));
}

Current topics : Compare two strings (urls) for same domain

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 12:14:54 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 12:14:40 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 12:14:21 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 12:14:02 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 12:13:51 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 12:13:41 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 12:13:36 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 12:13:22 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 12:13:09 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 12:12:49 PM

Top
© 2023 W3CODED - All Rights Reserved.