markrogoyski / ipv4-subnet-calculator-php

Network calculator for subnet mask and other classless (CIDR) network information.
MIT License
168 stars 42 forks source link

List All IPs #4

Closed decadence closed 5 years ago

decadence commented 7 years ago

Hello. Thanks for the library. Can we have method for listing all IP in subnet? Something like:

$sub = new IPv4\SubnetCalculator( '50.50.50.50', 20 );
list($start, $end) = $sub->getIPAddressRange();
$array = [];

$range = range(ip2long($start), ip2long($end));
foreach ($range as $rng){
    $array[] = long2ip($rng);
}
return $array;

If you like I can make PR for this or you can add it by yourself.

markrogoyski commented 7 years ago

Hi, Thank you for the suggestion. Would it be OK if this is a generator instead of returning an array of IPs? I think for a large subnet using an array may result in a PHP fatal error for allowed memory size.

decadence commented 7 years ago

I think that's will be ok. Anyway anyone can convert iterator to array if needed.

sohag1426 commented 7 years ago

Thanks decadence, you have told my requirement. I am searching a program that will list all the IPs in the subnet. And thanks to markrogoyski. Please include the feature if possible.

pedenski commented 6 years ago

I too would appreciate if this function is available. listing the IP range.

maikenunes commented 6 years ago

I would like to have that option, but I agree with @markrogoyski about the problem when we set 10 using the cidr notation, for example, will generate 2²² ips, so it will result in a fatal error.

markrogoyski commented 6 years ago

Thanks for all the feedback. I will try to include this in the next release.

markrogoyski commented 5 years ago

This has been added in version v3.0.0.

Thanks to @fillmorejd for implementing this!