public static function ip2bin($ip)
{
if (static::getIpVersion($ip) === self::IPV4) {
return str_pad(base_convert(ip2long($ip), 10, 2), self::IPV4_ADDRESS_LENGTH, '0', STR_PAD_LEFT);
}
$unpack = unpack('A16', inet_pton($ip));
$binStr = array_shift($unpack);
$bytes = self::IPV6_ADDRESS_LENGTH / 8; // 128 bit / 8 = 16 bytes
$result = '';
while ($bytes-- > 0) {
$result = sprintf('%08b', isset($binStr[$bytes]) ? ord($binStr[$bytes]) : '0') . $result;
}
return $result;
}
echo IpHelper::ip2bin('192.168.10.1');
00111111010101111111010111111111