1. Go to this page and download the library: Download matracine/ip-tools library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
matracine / ip-tools example snippets
use mracine\IPTools\IPv4\Address;
$address = new Address(0xff00ff00); // 255.0.255.0
$address = Address::fromString('10.11.12.13'); // From a dot quad string notation
$address = Address::fromArray([10, 11, 12, 13]); // From an Array of integers
$address = Address::fromArray(['10', '11', '12', '13']); // From an Array of strings
// Get a string with dot quad notation
$value = (string)$address;
$value = $address->asDotQuad();
// returns "1.2.3.4"
// Get the integer representation of address
$value = $address->asInteger();
$value = $address->int();
// get an addresse from another shifted by un offset (negateive or positive)
$address2 = $address->shift(10); // 10.0.0.0 => 10.0.0.10
$address2 = $address->shift(-10); // 10.0.0.0 => 9.255.255.246
// get next address
$address2 = $address->next(); // 10.0.0.0 => 10.0.0.1
// get previous address
$address2 = $address->previous(); // 10.0.0.0 => 9.255.255.255
// Test if two addresses are equivalent (same value)
if ($address->match($address2))
{...}
// Test if an address is in Range or Subnet
if($address->isIn($range))
{...}
// Address is part of RFC 1918 subnets ?
// see : https://tools.ietf.org/html/rfc1918
if($address->isRFC1918())
{...}
// Address is part of RFC 6598 subnet ?
// see : https://tools.ietf.org/html/rfc6598
if($address->isRFC6598())
{...}
// Address is multicast ?
// see : https://tools.ietf.org/html/rfc1112
if($address->isMulticast())
{...}
// get the CLASS of an address
// Obsolete notion for class routing
switch($addres->getClass())
{
case Address::CLASS_A:
...
break;
case Address::CLASS_B:
...
break;
case Address::CLASS_C:
...²
break;
case Address::CLASS_D:
...
break;
case Address::CLASS_E:
...
break;
}
use mracine\IPTools\IPv4\Netmask;
$netmask = new Netmask(0xffffff00); // 255.255.255.0
// trying to constrct a netmask with wrong value will throw an exception (\DomainException)
$netmask = new Netmask(0x12345678); // throws \DomainException
// Construct a Netmask from a CIDR notation
$netmask = Netmask::fromCidr(24); // '255.255.255.0'
$netmask = Netmask::fromCidr(-1); // throws \OutOfBoundsException
$netmask = Netmask::fromCidr(33); // throws \OutOfBoundsException
// Construct a Netmask from an Address
$netmask = Netmask::fromAddress(new Adress::fromString('255.255.255.0')); // 255.255.255.0
// In fact, Netmask extends Address, so you can use Address Helpers
$netmask = Netmask::fromString('255.255.255.0'); // 255.255.255.0
$netmask = Netmask::fromString('255.0.255.0'); // throws \DomainException, invalid netmask...
// Get the CIDR representation of Netmask (integer between 0 to 32
$cidr = $netmask->asCidr();
// Count the number of addresses in a netmask
// Imple!ents Countable
$count = count(Netmask::fromString('255.255.255.0')); // 256
$count = Netmask::fromString('255.255.255.0')->count(); // 256
// get a Netmask from another shifted by un offset (negateive or positive)
$netmask2 = $netmask->shift(1); // 255.255.255.0 => 255.255.255.128
$netmask2 = $netmask->shift(-1); // 255.255.255.0 => 255.255.254.0
// get next netmask
$netmask2 = $netmask->next(); // 255.255.255.0 => 255.255.255.128
// get previous netmask
$netmask = $netmask->previous(); // // 255.255.255.0 => 255.255.254.0
use mracine\IPTools\IPv4\Range;
$range = new Range(new Address('192.168.1.5'), new Address('192.168.1.34'));
$range = new Range(new Address('192.168.1.34'), new Address('192.168.1.5'));
$range = Range::fromCount(new Address('192.168.1.5'), 10); // From a lower bound, 10 addresses : 192.168.1.5-192.168.1.14
$range = Range::fromCount(new Address('192.168.1.5'), -5); // From a uppen bound, 10 addresses : 192.168.0.252-192.168.1.5
// Retreive the first Address
$address = $range->getUpperBound();
// Retreive the last Address
$address = $range->getLowerBound();
// Implements ArrayAccess
$range = new Range(new Address('192.168.1.5'), new Address('192.168.1.10')); // Same as constructor
$address = $range[0]; // 192.168.1.5
$address = $range[2]; // 192.168.1.7
// Trying to access an address out of the range will raise an exception
$address = $range[-1]; // exception
$address = $range[50]; // exception
// Read only : Trying to affect or unset an item will raise an exception
$range[1] = $address; // exception
unset($range[1]); // exception
// get a range from another shifted by an offset (negateive or positive), keeping the same address count
$range2 = $range1->shift(2); // 192.168.1.10-192.168.1.15 => 192.168.1.27-192.168.1.32
$range2 = $range1->shift(-1); // 192.168.1.10-192.168.1.15 => 192.168.1.9-192.168.1.4
// get next adjacent range, with same address count
$range2 = $range->next(); // 192.168.1.0-192.168.1.9 => 192.168.1.10-192.168.1.19
// get previous adjacent range, with same address count
$range2 = $range->previous(); // 192.168.1.0-192.168.1.9 => 192.168.0.246-192.168.0.255
// Iterate all the addresses of the range
foreach($subnet as $address)
{...}
// true if 2 ranges are equivalent (same pper and lower bounds)
if($range->match($range2))
{...}
// Check if an Address is contained in a range
if($range->contains($address))
{...}
// Check if a range is contained in an other range
if($range->isIn($range2))
{...}
// Get count of addresses in a range
$addressCount = count($range); // Interface Countable
$addressCount = $range->count();
use mracine\IPTools\IPv4\Subnet;
$subnet = new Subnet(new Address('192.168.1.0'), new Netmask('255.255.255.250'));
$subnet = new Subnet(new Address('192.168.1.50'), new Netmask('255.255.255.250'), false); // network address will be calculated to 192.168.1.0
// Creates a subnet from CIDR notation
$subnet = Subnet::fromCidr(new Address('192.168.1.0'), 24);
// Creates a subnet from a string formated with CIDR or address/netmask notation
$subnet = Subnet::fromString('192.168.1.0/24');
$subnet = Subnet::fromString('192.168.1.0/255.255.255.0');
// Retreive the network as an Address object
$address = $ubnet->getNetworkAddress(); // 192.168.1.0
// Retreive the netmask as a Netmask object
$netmask = $ubnet->getNetmaskAddress(); // 255.25.255.0
// Retreive the broadcast address as an Address object
$address = $ubnet->getBroadcastAddress(); // 192.168.1.255
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.