(int), 'end' => (int)] representing * the according start and end addresses as integer * values. Returns false on malformed input. * @param string $cidr 192.168.101/24, 1.2.3.4/16, ... * @return array|false start and end address, false on error */ public static function parseCidr($cidr) { $parts = explode('/', $cidr); if (count($parts) !== 2) { $ip = ip2long($cidr); if ($ip === false) return false; if (PHP_INT_SIZE === 4) { $ip = sprintf('%u', $ip); } return ['start' => $ip, 'end' => $ip]; } $ip = $parts[0]; $bits = $parts[1]; if (!is_numeric($bits) || $bits < 0 || $bits > 32) return false; $dots = substr_count($ip, '.'); if ($dots < 3) { $ip .= str_repeat('.0', 3 - $dots); } $ip = ip2long($ip); if ($ip === false) return false; $bits = pow(2, 32 - $bits) - 1; if (PHP_INT_SIZE === 4) return ['start' => sprintf('%u', $ip & ~$bits), 'end' => sprintf('%u', $ip | $bits)]; return ['start' => $ip & ~$bits, 'end' => $ip | $bits]; } }