IpUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2009 by Denis M. Gabaidulin                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class IpUtils extends StaticFactory
00016     {
00017         public static function makeRanges(array $ips)
00018         {
00019             $ipsAsIntegers = array();
00020 
00021             foreach ($ips as $ip)
00022                 $ipsAsIntegers[] = ip2long($ip);
00023 
00024             sort($ipsAsIntegers);
00025             
00026             $size = count($ipsAsIntegers);
00027 
00028             $ranges = array();
00029 
00030             $j = 0;
00031 
00032             $ranges[$j][] = long2ip($ipsAsIntegers[0]);
00033 
00034             for ($i = 1; $i < $size; ++$i) {
00035                 if ($ipsAsIntegers[$i] != $ipsAsIntegers[$i - 1] + 1) {
00036                     $ranges[++$j][] = long2ip($ipsAsIntegers[$i]); // start new range
00037                 } else
00038                     $ranges[$j][] = long2ip($ipsAsIntegers[$i]);
00039             }
00040             
00041             return $ranges;
00042         }
00043     }
00044 ?>