3v4l.org

run code in 300+ PHP versions simultaneously
<?php function esip($ip_addr) { //first of all the format of the ip address is matched if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$ip_addr)) { //now all the intger values are separated $parts=explode(".",$ip_addr); //now we need to check each part can range from 0-255 foreach($parts as $ip_parts) { if(intval($ip_parts)>255 || intval($ip_parts)<0) return FALSE; //if number is not within range of 0-255 } return TRUE; } else return FALSE; //if format of ip address doesn't matches } function domain($domainb) { $bits = explode('/', $domainb); if ($bits[0]=='http:' || $bits[0]=='https:') { $domainb= $bits[2]; } else { $domainb= $bits[0]; } unset($bits); $bits = explode('.', $domainb); $idz=count($bits); $idz-=3; if (strlen($bits[($idz+2)])==2) { $url=$bits[$idz].'.'.$bits[($idz+1)].'.'.$bits[($idz+2)]; } else if (strlen($bits[($idz+2)])==0) { $url=$bits[($idz)].'.'.$bits[($idz+1)]; } else { $url=$bits[($idz+1)].'.'.$bits[($idz+2)]; } return $url; } $address='clients1.sub3.google.co.uk'; $parsed_url = parse_url($address); $check = esip($parsed_url['host']); $host = $parsed_url['host']; if ($check == FALSE){ if ($host != ""){ $host = domain($host); }else{ $host = domain($address); } } echo $host;
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: Undefined array key "host" in /in/EhD5r on line 48 Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in /in/EhD5r on line 5 Warning: Undefined array key "host" in /in/EhD5r on line 49 google.co.uk
Output for 8.0.0 - 8.0.30
Warning: Undefined array key "host" in /in/EhD5r on line 48 Warning: Undefined array key "host" in /in/EhD5r on line 49 google.co.uk
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
Notice: Undefined index: host in /in/EhD5r on line 48 Notice: Undefined index: host in /in/EhD5r on line 49 google.co.uk
Output for 7.3.32 - 7.3.33
google.co.uk
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
Notice: Undefined index: host in /in/EhD5r on line 48 Notice: Undefined index: host in /in/EhD5r on line 49 google.co.uk

preferences:
269.78 ms | 403 KiB | 370 Q