php cidr prefix to netmask

phpnoobipv4 picture phpnoobipv4 · Apr 19, 2011 · Viewed 9.9k times · Source

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?

Answer

jonavon picture jonavon · May 2, 2011

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 at snipplr.