PHP code example of luka / network-address-types
1. Go to this page and download the library: Download luka/network-address-types 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/ */
luka / network-address-types example snippets
use LUKA\Network\NetworkAddress;
$address = NetworkAddress::fromString('127.0.0.1');
assert($address instanceof \LUKA\Network\IPv4\IPv4Address);
$address = NetworkAddress::fromString('127.0.0.1/8');
assert($address instanceof \LUKA\Network\IPv4\CIDRv4Address);
$address = NetworkAddress::fromString('::1');
assert($address instanceof \LUKA\Network\IPv6\IPv6Address);
$address = NetworkAddress::fromString('ff80::1/64');
assert($address instanceof \LUKA\Network\IPv6\CIDRv6Address);
$address = NetworkAddress::fromString('84:34:ff:ff:ff:ff');
assert($address instanceof \LUKA\Network\MACAddress);
use LUKA\Network\NetworkAddress;
assert('::1' === NetworkAddress::fromString('::1')->toString());
use LUKA\Network\NetworkAddress;
assert('::1' === NetworkAddress::fromString('0:0:0:0::1')->toString());
assert('00:00:00:00:00:00' === NetworkAddress::fromString('00-00-00-00-00-00')->toString());
use LUKA\Network\NetworkAddress;
assert(
true === NetworkAddress::fromString('::1')
->equals(NetworkAddress::fromString('::1'))
);
// Value mismatch:
assert(
false === NetworkAddress::fromString('::1')
->equals(NetworkAddress::fromString('::2'))
);
// Type mismatch (different IP version):
assert(
false === NetworkAddress::fromString('::1')
->equals(NetworkAddress::fromString('127.0.0.1'))
);
// Type mismatch (cidr vs non-cidr)
assert(
false === NetworkAddress::fromString('192.168.0.5')
->equals(NetworkAddress::fromString('192.168.0.5/24'))
);
use LUKA\Network\NetworkAddress;
$cidr = NetworkAddress::fromString('192.168.0.7/8');
$network = $cidr->toNetwork();
assert(true === $network->containsAddress(NetworkAddress::fromString('192.168.0.1')));
assert(true === $network->containsAddress(NetworkAddress::fromString('192.45.0.2')));
assert(false === $network->containsAddress(NetworkAddress::fromString('127.10.0.1')));
assert(false === $network->containsAddress(NetworkAddress::fromString('ff80::5')));
use LUKA\Network\NetworkAddress;
$cidr = NetworkAddress::fromString('ff80::/64');
$network = $cidr->toNetwork();
assert(true === $network->containsAddress(NetworkAddress::fromString('ff80::1')));
assert(true === $network->containsAddress(NetworkAddress::fromString('ff80::10:e5:7')));
assert(false === $network->containsAddress(NetworkAddress::fromString('ff80:e::1')));
assert(false === $network->containsAddress(NetworkAddress::fromString('127.0.0.1')));