Back to the module list

IP addresses calculation

The IP addresses in IPv4 and IPv6 are complex to manage. In cause, the differents allowed formats.

The module allow to uncompress an IPv6 address (::1 is converted to 0000:0000:0000:0000:0000:0000:0000:0001), reverse an IP address (to use in DNS server in Reverse zones), calculate a CIDR associated to a IPv4 netmask

The class definition

Class \ipaddresses

Namespace \


 Manage the IP addresses conversions


No property available


public function cidrToBin ($cidr, $length)
 Return the provided CIDR in binary. Length must be in bytes.
 Return FALSE if the parameters are invalid
 @param integer $cidr The CIDR to convert
 @param integer $length The length to use

public function completeAddressWithZero ($ip)
 Return the IP adddress with filling the fields with the missing zeros.
 Valid only on IPv6 (but don't change anything if the provided address is
 @param string $ip The IP to complete

public function groupIPv6 ($ipv6)
 Get an IPv6 address with the format
 and return it with format
 Return false if the IP provided is not complete
 @param string $ipv6 The IPv6 to group

public function netmask2cidr ($netmask)
 This function return the CIDR associated to the provided netmask
 Ex. Return 24 for a mask
 Work only in IPv4
 Return FALSE if the provided IP is invalid
 @param string $netmask The mask to convert in CIDR

public function reverseIPAddress ($ipReverse)
 Reverse the provided IP address
 The IPv6 are returned in format :
 @param string $ipReverse The IPv6 to reverse

public function str_base_convert ($str, $frombase=10, $tobase=36)
 Base conversion with 128 bits support for IPv6
 Based on
 @param string $str The string to convert
 @param integer|null $frombase The base of the provided string (10 by
 @param integer|null $tobase The base of the returned string (36 by

public function uncompressIPv6 ($ip)
 Return the IPv6 uncompressed (all the fields exists). They are not filled
 by zeros
 If the provided IP is IPv4, there is no change applied
 Return False if the parameter is invalid
 Based on
 @param string $ip The IP address to uncompress

public function validIPAddress ($ip)
 Return true if the provided IP address is valid (IPv4 or IPv6)
 @param string $ip The IP Address to validate

public function validIPv4Address ($ip)
 Return true if the provided IP address is valid and is IPv4
 @param string $ip The IP Address to validate

public function validIPv6Address ($ip)
 Return true if the provided IP address is valid and is IPv6
 @param string $ip The IP Address to validate