3v4l.org

run code in 300+ PHP versions simultaneously
<?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); $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']; } $url = rtrim($url, '/'); var_dump($url); return $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.3.0 - 8.3.4
string(35) "seos.bolseo.ru/?module=2&siteID=235" Warning: Trying to access array offset on false in /in/dIqXC on line 39 Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /in/dIqXC on line 13 Warning: Trying to access array offset on false in /in/dIqXC on line 40 Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /in/dIqXC on line 40 string(0) "" bool(false)
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17
string(35) "seos.bolseo.ru/?module=2&siteID=235" Warning: Trying to access array offset on value of type bool in /in/dIqXC on line 39 Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /in/dIqXC on line 13 Warning: Trying to access array offset on value of type bool in /in/dIqXC on line 40 Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /in/dIqXC on line 40 string(0) "" bool(false)
Output for 8.0.0 - 8.0.30
string(35) "seos.bolseo.ru/?module=2&siteID=235" Warning: Trying to access array offset on value of type bool in /in/dIqXC on line 39 Warning: Trying to access array offset on value of type bool in /in/dIqXC on line 40 string(0) "" bool(false)
Output for 7.4.0 - 7.4.33
string(35) "seos.bolseo.ru/?module=2&siteID=235" Notice: Trying to access array offset on value of type bool in /in/dIqXC on line 39 Notice: Trying to access array offset on value of type bool in /in/dIqXC on line 40 string(0) "" bool(false)
Output for 5.3.3 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
string(35) "seos.bolseo.ru/?module=2&siteID=235" string(0) "" bool(false)
Output for 5.2.0 - 5.2.17, 5.3.0 - 5.3.2
string(35) "seos.bolseo.ru/?module=2&siteID=235" Warning: parse_url(http:////seos.bolseo.ru/?module=2&siteID=235): Unable to parse URL in /in/dIqXC on line 37 string(0) "" bool(false)
Output for 5.1.2 - 5.1.6
string(35) "seos.bolseo.ru/?module=2&siteID=235" Warning: parse_url(http:////seos.bolseo.ru/?module=2&siteID=235): Unable to parse url in /in/dIqXC on line 37 string(0) "" bool(false)
Output for 4.3.4 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.1
Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/dIqXC on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dIqXC on line 33 string(41) "http//seos.bolseo.ru/?module=2&siteID=235" Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/dIqXC on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dIqXC on line 33 Warning: parse_url(http:////seos.bolseo.ru/?module=2&siteID=235): Unable to parse url in /in/dIqXC on line 37 string(0) "" bool(false)
Output for 4.3.2 - 4.3.3
Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/dIqXC on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dIqXC on line 33 Warning: parse_url(http://http://seos.bolseo.ru/index.php?module=2&siteID=235): Unable to parse url in /in/dIqXC on line 37 string(0) "" Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/dIqXC on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dIqXC on line 33 Warning: parse_url(http:////seos.bolseo.ru/?module=2&siteID=235): Unable to parse url in /in/dIqXC on line 37 string(0) "" bool(true)
Output for 4.3.0 - 4.3.1
Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/dIqXC on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dIqXC on line 33 Warning: parse_url(http://http://seos.bolseo.ru/index.php?module=2&siteID=235) [http://www.php.net/function.parse-url]: Unable to parse url in /in/dIqXC on line 37 string(0) "" Notice: Use of undefined constant PHP_URL_SCHEME - assumed 'PHP_URL_SCHEME' in /in/dIqXC on line 33 Warning: parse_url() expects exactly 1 parameter, 2 given in /in/dIqXC on line 33 Notice: Undefined index: path in /in/dIqXC on line 40 string(0) "" bool(true)

preferences:
235.42 ms | 402 KiB | 338 Q