Skip to main content

CIDR Notation To IP Range

Overview

This document provides information about CIDR Notation To IP Range.

function cidrToRange($cidr)
{
$range = [];
$cidr = explode('/', $cidr);
$range['from'] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int) $cidr[1]))));
$range['to'] = long2ip((ip2long($range['from'])) + pow(2, (32 - (int) $cidr[1])) - 1);

return ['cidr' => implode('/', $cidr)] + $range;
}


// "INSERT INTO `users_ip_ranges` (`tenant_id`, `user_id`, `ip_from`, `ip_to`, `updated_at`, `created_at`) VALUES ('341', '62896', '201.234.241.72', '201.234.241.79', NOW(), NOW());";

echo(
collect([
'201.234.241.72/29',
'190.66.1.32/27',
'190.242.103.32/28',
'200.3.144.0/23',
'64.76.191.200/29',
'190.131.229.80/29',
'190.14.230.224/29',
'190.90.156.49/29',
'190.0.26.113/29',
'201.234.242.160/28',
'181.143.83.240/29',
'207.248.81.0/24',
'190.254.6.0/24',
'190.242.43.224/29',
])->map(function ($cidr) {
$range = cidrToRange($cidr);

// return $range;

return "INSERT INTO `users_ip_ranges` (`tenant_id`, `user_id`, `ip_from`, `ip_to`, `updated_at`, `created_at`) VALUES ('341', '62896', '".$range['from']."', '".$range['to']."', NOW(), NOW());";
})->flatten()->toJson()
// cidrToRange('201.234.241.72/29')
);

X

Graph View