How to create a range of IP addresses?

Suppose the input is:

222.123.34.45 and 222.123.34.55

then I need to output the ip address between them:

222.123.34.45 222.123.34.46 ... 222.123.34.55
+3
source share
2 answers

Use ip2long()and long2ip():

function ip_range($from, $to) {
  $start = ip2long($from);
  $end = ip2long($to);
  $range = range($start, $end);
  return array_map('long2ip', $range);
}

The above turns two IP addresses into numbers (using the basic functions of PHP), creates a range of numbers, and then turns this range of numbers into IP addresses.

If you want them to be separated by spaces, just the implode()result.

+20
source

Using flexibility like PHP

Using the fact that IP addresses are actually numbers (can do strange things on

  $ ip2 = '222.123.34.55';

$ips = array();
for($i=ip2long($ip);$i<=ip2long($ip2);$i++)
{
    $ips[] = long2ip($i);
}

print_r($ips);
0
source

Source: https://habr.com/ru/post/1726203/


All Articles