Added cidr2mask()

This commit is contained in:
billz 2020-11-30 12:09:56 +00:00
parent 5cc7794cb0
commit 5b1325803a
1 changed files with 21 additions and 0 deletions

View File

@ -1,6 +1,12 @@
<?php
/* Functions for Networking */
/**
* Converts a netmask to CIDR notation string
*
* @param string $mask
* @return string
*/
function mask2cidr($mask)
{
$long = ip2long($mask);
@ -8,6 +14,21 @@ function mask2cidr($mask)
return 32-log(($long ^ $base)+1, 2);
}
/**
* Converts a CIDR notation string to a netmask
*
* @param string $cidr
* @return string
*/
function cidr2mask($cidr)
{
$ta = substr ($cidr, strpos ($cidr, '/') + 1) * 1;
$netmask = str_split (str_pad (str_pad ('', $ta, '1'), 32, '0'), 8);
foreach ($netmask as &$element)
$element = bindec ($element);
return join ('.', $netmask);
}
/* Functions to write ini files */
function write_php_ini($array, $file)