PHP Classes

Aggregator: Aggregate multiple IP addresses in single ranges

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 130 All time: 9,348 This week: 74Up
Version License PHP version Categories
aggregator 1.0.0Custom (specified...5Networking, PHP 5


This package can aggregate multiple IP addresses in single ranges.

It takes an array of IP addresses or ranges and merges those that are contiguous to reduce the number of ranges that may represent the same IP addresses.

The class supports both IPv4 and IPv6 IP addresses.

Picture of Caleb
Name: Caleb <contact>
Classes: 4 packages by
Country: Australia Australia
Age: ???
All time rank: 347142 in Australia Australia
Week rank: 280 Up5 in Australia Australia Up
Innovation award
Innovation award
Nominee: 2x


Join the chat at PHP >= 5.4.0 License: GPL v2 PRs Welcome


A stand-alone class implementation of the IPv4+IPv6 IP+CIDR aggregator from CIDRAM.

How to install:

As a stand-alone PHP class, installing it is exceptionally easy. You can download the file containing the class, Aggregator.php, directly from this repository, and copy it to any projects that need it, or, if you'd prefer, you can install it using Composer:

composer require cidram/aggregator

Note: The code in this class is based upon code in the CIDRAM package, but the two are NOT dependent on each other.

After you've downloaded the file, to allow your projects to use the class, PSR-4 autoloading is preferred (particularly if you're using a large number of different, unrelated classes). If you're installing the class via Composer, all you have to do is require_once 'vendor/autoload.php'; and everything will be taken care of. Alternatively, if you're installing it manually (or without Composer), and don't want to use a PSR-4 autoloader, you can simply require or include the class into your projects (which may be much easier in many cases) by including the respective statement to point to the class file in the relevant PHP files.

How to use:

The simplest way to use Aggregator is to create a new instance of the class and enter some data to be aggregated as a parameter to the aggregate method. The aggregate method will return an aggregate of the entered data.


use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Output = $Aggregator->aggregate($Input);

Or, if the file helpers.php is loaded, this function will be available:

$Output = aggregate($Input);

Note: The function aggregate will be available if you installed the package via composer, but otherwise, you'll need to include the file src/helpers.php.

In the case of the above example, if this is entered as $Input: Some arbitrary single IPs from here
2002::1 Some arbitrary INVALID single IPs from here
2002:abcd:efgh::1 Some arbitrary CIDRs from here Some arbitrary INVALID CIDRs from here Some arbitrary netmasks from here Some arbitrary INVALID netmasks from here
Foobar Some garbage data from here

$Output will be expected to contain this:

Data is newline-delimited and each line represents one item to be aggregated. Aggregator handles IPv4+IPv6 seamlessly, attempts to clean up each item (i.e., remove invalid and superfluous data in order to reduce to a valid IP, CIDR, or netmask), attempts to aggregate the resultant cleaned up data (unreadable and invalid data is rejected), and then returns the resultant aggregated data.

It is possible to obtain more information about each aggregation operation if desired. If "Results" is set to true (it is false by default), then "NumberEntered" (the total number of lines entered when an operation begins), "NumberRejected" (the number of lines or items "rejected", i.e., perceived as invalid, or unreadable; note that this number will also also duplicate items, due to that duplicates are stripped along with invalid and superfluous data prior to aggregation), "NumberAccepted" (the number of lines or items accepted for aggregation; i.e., NumberAccepted = NumberEntered - NumberRejected), "NumberMerged" (the total number of items aggregated or merged), and "NumberReturned" (the total number of items returned at the end of an operation) will be populated during operation accordingly. These values can be retrieved after each operation from the class instance or object:

use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Aggregator->Results = true;
$Output = $Aggregator->aggregate($Input);
echo $Output;
echo "\n\n";
echo $Aggregator->NumberEntered . "\n";
echo $Aggregator->NumberRejected . "\n";
echo $Aggregator->NumberAccepted . "\n";
echo $Aggregator->NumberMerged . "\n";
echo $Aggregator->NumberReturned . "\n";

Generally, it is better to create a new class instance for each aggregation operation. However, if you want to recycle an old instance, and want to continue to retrieve these values after each operation, you can reset these values to their initial state between operations by using the "resetNumbers" method:


Regardless of whether "Results" is true or false, after each aggregation operation, "ProcessingTime" will be available, in case you want to know how much was consumed during the operation. This could be useful both for debugging and for general vanity purposes.


use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator();
$Output = $Aggregator->aggregate($Input);
echo $Aggregator->ProcessingTime . "\n";

Additionally, "ExpandIPv4" and "ExpandIPv6" public methods are provided with the class, and they function in exactly the same way their CIDRAM package closure counterparts. Calling either of these with an IPv4 or IPv6 IP address respectively will return an array containing the potential factors for the given IP address. The potential factors are all possible subnets (or CIDRs) that the given IP address is a member of. When a valid IP address is supplied, "ExpandIPv4" and "ExpandIPv6" and should return an array with 32 and 128 elements respectively.

If you want Aggregator to return results as netmasks instead of CIDRs, you can instantiate the object with a parameter value of 1, like this:

use \CIDRAM\Aggregator\Aggregator;

$Aggregator = new Aggregator(1);
$Output = $Aggregator->aggregate($Input);

In the case of the example input mentioned earlier, the output should look something like this:

Other information:


Licensed as GNU General Public License version 2.0 (GPLv2).

For support:

Please use the issues page of this repository.

Last Updated: 9 January 2019 (2019.01.09).

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file Changelog.txt Doc. Documentation
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Aggregator.php Class Class source
  Accessible without login Plain text file helpers.php Example Example script

  Files folder image Files  /  tests  
File Role Description
  Plain text file AggregatorTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:9,348
This week:74Up