I'm looking to convert a cidr prefix (e.g. /28) to a netmask (e.g. 255.255.255.240) and have not located a function for this, does one exist in php? If not, how would I go about doing this?
The CIDR number comes from the number of ones in the subnet mask when converted to binary. The subnet mask 255.255. 255.0 is 11111111.11111111.
Class B IP Addresses 255.255 , with a default subnet mask of 255.255. 0.0 (or /16 in CIDR).
I wrote a class that contains a method that does this.
function CIDRtoMask($int) {
return long2ip(-1 << (32 - (int)$int));
}
I posted the entire class as a gist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With