<?php
/**
* Return domain name from url
* For example: "all.ru" from "http://www.all.ru/test/test.html"
*
* @param $url
* @param bool $with_www
* @return string : domain name
*/
function get_domain($url, $with_www = false)
{
$url = preg_replace('#^https?://#', '', trim($url));
$path = explode ('/', $url);
$domain = strtolower(trim($path[0]));
$domain = preg_replace('#^www.#', '', $domain);
if($with_www) {
$domain = 'www.' . $domain;
}
return $domain;
}
/**
* Format URL
*
* @param $url
* @return string
*/
function format_url($url) {
$parsed_url = parse_url($url);
var_dump($parsed_url);
$host = get_domain($parsed_url['host'], false);
$path = preg_replace('#index\.[a-z]+#', '', $parsed_url['path']);
$url = $host . $path;
if (isset($parsed_url['query'])) {
$url .= '?' . $parsed_url['query'];
}
return rtrim($url, '/');
}
/**
* Compare two formatted urls
*
* @param string $url_1
* @param string $url_2
* @return bool
*/
function compare_urls($url_1, $url_2) {
return format_url($url_1) === format_url($url_2);
}
$url_1 = 'http://seos.bolseo.ru/index.php?module=2&siteID=235';
$url_2 = 'seos.bolseo.ru/?module=2&siteID=235';
var_dump(compare_urls($url_1, $url_2));
- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "seos.bolseo.ru"
["path"]=>
string(10) "/index.php"
["query"]=>
string(19) "module=2&siteID=235"
}
array(2) {
["path"]=>
string(15) "seos.bolseo.ru/"
["query"]=>
string(19) "module=2&siteID=235"
}
Warning: Undefined array key "host" in /in/rbYIc on line 37
Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /in/rbYIc on line 13
bool(true)
- Output for 8.0.0 - 8.0.30
- array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "seos.bolseo.ru"
["path"]=>
string(10) "/index.php"
["query"]=>
string(19) "module=2&siteID=235"
}
array(2) {
["path"]=>
string(15) "seos.bolseo.ru/"
["query"]=>
string(19) "module=2&siteID=235"
}
Warning: Undefined array key "host" in /in/rbYIc on line 37
bool(true)
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
- array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "seos.bolseo.ru"
["path"]=>
string(10) "/index.php"
["query"]=>
string(19) "module=2&siteID=235"
}
array(2) {
["path"]=>
string(15) "seos.bolseo.ru/"
["query"]=>
string(19) "module=2&siteID=235"
}
Notice: Undefined index: host in /in/rbYIc on line 37
bool(true)
- Output for 7.3.32 - 7.3.33
- array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "seos.bolseo.ru"
["path"]=>
string(10) "/index.php"
["query"]=>
string(19) "module=2&siteID=235"
}
array(2) {
["path"]=>
string(15) "seos.bolseo.ru/"
["query"]=>
string(19) "module=2&siteID=235"
}
bool(true)
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(14) "seos.bolseo.ru"
["path"]=>
string(10) "/index.php"
["query"]=>
string(19) "module=2&siteID=235"
}
array(2) {
["path"]=>
string(15) "seos.bolseo.ru/"
["query"]=>
string(19) "module=2&siteID=235"
}
Notice: Undefined index: host in /in/rbYIc on line 37
bool(true)
preferences:
247.53 ms | 403 KiB | 371 Q