Skip to content

Instantly share code, notes, and snippets.

@SaigyoujiYuyuko233
Created May 7, 2020 16:00
Show Gist options
  • Save SaigyoujiYuyuko233/6320b34bc3e0e7e73e61b71e910104ed to your computer and use it in GitHub Desktop.
Save SaigyoujiYuyuko233/6320b34bc3e0e7e73e61b71e910104ed to your computer and use it in GitHub Desktop.
Covert netmask(255.255.255.0) to cidr(/24)
function netmask2Cidr($startIP = "10.198.12.0", $endIP = "10.198.12.255") {
$margin = strlen(decbin((ip2long($endIP) - ip2long($startIP))));
$netmask = decbin(0xFFFFFFFF << $margin & 0xFFFFFFFF);
return substr_count($netmask, "1");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment