3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $testArray = array( 'sub1.sub2.example.co.uk', 'sub1.example.com', 'example.com', 'sub1.sub2.sub3.example.co.uk', 'sub1.sub2.sub3.example.com', 'sub1.sub2.example.com' ); foreach($testArray as $k => $v) { echo $k." => ".extract_domain($v)."\n"; } function extract_domain($domain) { if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches)) { return $matches['domain']; } else { return $domain; } } function extract_subdomains($domain) { $subdomains = $domain; $domain = extract_domain($subdomains); $subdomains = rtrim(strstr($subdomains, $domain, true), '.'); return $subdomains; }
based on oO89P
Output for 4.3.3 - 7.1.0
0 => example.co.uk 1 => example.com 2 => example.com 3 => example.co.uk 4 => example.com 5 => example.com
Output for 4.3.0 - 4.3.2
Notice: Undefined index: domain in /in/JkNf2 on line 20 0 => Notice: Undefined index: domain in /in/JkNf2 on line 20 1 => Notice: Undefined index: domain in /in/JkNf2 on line 20 2 => Notice: Undefined index: domain in /in/JkNf2 on line 20 3 => Notice: Undefined index: domain in /in/JkNf2 on line 20 4 => Notice: Undefined index: domain in /in/JkNf2 on line 20 5 =>