Skip to content

Instantly share code, notes, and snippets.

@realeroberto
Created July 11, 2014 15:42
Show Gist options
  • Save realeroberto/5394beeb2173f7b74f9e to your computer and use it in GitHub Desktop.
Save realeroberto/5394beeb2173f7b74f9e to your computer and use it in GitHub Desktop.
Convert a Classless Inter-Domain Routing prefix to a plain old network mask.
<?php
/*
* Convert a Classless Inter-Domain Routing prefix to a plain old network mask.
*
* E.g.: 24 => 255.255.255.0
*/
function cidr_prefix_to_netmask($prefix)
{
for ($i = 0, $netmask = 0; $i <= (int)$prefix; $i++, $netmask += 2 << (32 - $i));
return long2ip($netmask);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment