IP address handling library

0.2 2016-02-26 12:05 UTC


This library allows you to handle IP addresses in various forms (currently IPv4 only) and test whether an IP matches certain constraints (equality, in a subnet or in a range).



use IPAddress\IPv4\Address;
use IPAddress\IPv4\Subnet;

$address = new Address(""); // These statements yield the same address
$address = new Address(0x010208c8);
$address = new Address(array(1, 2, 8, 200));
$address = new Address(array("1", "2", "010", "0xc8"));

$subnet = new Subnet("", ""); // These statements yield the same subnet
$subnet = Subnet::fromCidr("", 16);
$subnet = Subnet::fromString("");
$subnet = Subnet::fromString("");

if($address->isPrivate()) {
    echo "Your address is in one of the RFC1918 Private networks.\n";

if($subnet->match($address)) {
    echo "I know your address is in the \"$subnet\" subnet.\n";
    // OUTPUT: I know your address is in the " netmask" subnet.

The library's test have been run on both 32-bit and 64-bit php and should work on both. Please note that the Address class' int() method (which returns the integer representation of an IP address) will return a negative number for ip addresses larger than on 32-bit php due to the fact that php doesn't support unsigned integers.


  • More utility methods
  • IPv6 support


I have limited knowledge of IP addresses so I could have made some mistakes regarding terminology or even behavior of the library in certain cases.