3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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('~^(?:f|ht)tps?://~i', '', 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) { if(parse_url($url, PHP_URL_SCHEME) === null) { $url = 'http://' . $url; //parse_url() requires scheme to parse URL correctly } $parsed_url = parse_url($url); $formatted_url = get_domain($parsed_url['host'], false); if (isset($parsed_url['path'])) { $formatted_url .= preg_replace('#index\.[a-z]+#', '', $parsed_url['path']); } if (isset($parsed_url['query'])) { $url .= '?' . $parsed_url['query']; } $formatted_url = rtrim($formatted_url, '/'); var_dump($formatted_url); return $formatted_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 = 'https://seos.bolseo.ru/index.php?'; $url_2 = 'seos.bolseo.ru'; var_dump(compare_urls($url_1, $url_2));
Output for 5.1.2 - 7.1.0
string(14) "seos.bolseo.ru" string(14) "seos.bolseo.ru" bool(true)
Output for 4.3.4 - 5.1.1
Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/cCsCU on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/cCsCU on line 33 string(21) "https//seos.bolseo.ru" Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/cCsCU on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/cCsCU on line 33 string(14) "seos.bolseo.ru" bool(false)
Output for 4.3.2 - 4.3.3
Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/cCsCU on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/cCsCU on line 33 Warning: parse_url(http://https://seos.bolseo.ru/index.php?): Unable to parse url in /in/cCsCU on line 37 string(0) "" Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/cCsCU on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/cCsCU on line 33 string(14) "seos.bolseo.ru" bool(false)
Output for 4.3.0 - 4.3.1
Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/cCsCU on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/cCsCU on line 33 Warning: parse_url(http://https://seos.bolseo.ru/index.php?) [http://www.php.net/function.parse-url]: Unable to parse url in /in/cCsCU on line 37 string(0) "" Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/cCsCU on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/cCsCU on line 33 string(14) "seos.bolseo.ru" bool(false)