tags:

views:

126

answers:

2

Suppose the input is:

222.123.34.45 and 222.123.34.55

then I need to output the ip address in between them:

222.123.34.45 222.123.34.46 ... 222.123.34.55
+14  A: 

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 the two IP addresses into numbers (using PHP core functions), creates a range of numbers and then turns that number range into IP addresses.

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

cletus
Great answer ~!
Tegeril
+1 for pointing out three functions i'd never seen before. Might be better just to iterate `$start` to `$end` rather than potentially creating gigantic arrays.
Peter Kovacs
ditto, I definitely just learned 2 new funcs, thanks cletus!
Dan Beam
It's one of the great things about PHP: there are functions for almost everything already.
cletus
A: 
Alan Storm