2 ){ $domainExtensions = array_slice( $hostParts, -2 ); } if( count( $hostParts ) === 3 ){ if( strlen( implode( '', $domainExtensions ) ) > 5 ){ $hostParts = $domainExtensions; } } elseif( count( $hostParts ) > 3 ){ // skip ips if( ! is_numeric( implode( '', $hostParts ) ) ){ // if one of the last two parts is domain if( strlen( implode( '', $domainExtensions ) ) > 5 ){ $hostParts = $domainExtensions; } else { $hostParts = array_slice( $hostParts, -3 ); } } } $host = implode( '.', $hostParts ); return trim( $host, '/' ); } }