Skip to content

Commit

Permalink
Added functions to compress and expand IPv6 address.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Feb 23, 2022
1 parent c9ac763 commit 0d83313
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Below is the description of the functions available in the **IpTools** class.
| **array** cidrToIpv4($cidr) | Convert IPv4 CIDR notation into a list of IPv4 addresses. |
| **array** ipv6ToCidr($ipFrom, $ipTo) | Convert IPv6 range into a list of IPv6 CIDR notation. |
| **array** cidrToIpv6($cidr) | Convert IPv6 CIDR notation into a list of IPv6 addresses. |
| **string** compressIpv6($ipv6) | Compress a IPv6 to shorten the length. |
| **string** expandIpv6($ipv6) | Expand a shorten IPv6 to full length. |

### Country Class

Expand Down
12 changes: 11 additions & 1 deletion examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,17 @@

// Convert IPv6 range into CIDR
echo '<pre>';
print_r($ipTools->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0101', '2002:0000:0000:1234:ffff:ffff:ffff:ffff'));
print_r($ipTools->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff'));
echo '</pre>';

// Compress IPv6
echo '<pre>';
print_r($ipTools->compressIpv6('2002:0000:0000:1234:FFFF:FFFF:FFFF:FFFF'));
echo '</pre>';

// Expand IPv6
echo '<pre>';
print_r($ipTools->compressIpv6('2002::1234:FFFF:FFFF:FFFF:FFFF'));
echo '</pre>';

echo '<br>';
Expand Down
12 changes: 12 additions & 0 deletions src/IpTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ public function bin2Ip($bin)
return inet_ntop(inet_pton(substr($ipv6, 0, -1)));
}

public function compressIpv6($ipv6)
{
return inet_ntop(inet_pton($ipv6));
}

public function expandIpv6($ipv6)
{
$hex = unpack('H*0', inet_pton($ipv6));

return implode(':', str_split($hex[0], 4));
}

private function ip2Bin($ip)
{
if (($n = inet_pton($ip)) === false) {
Expand Down
40 changes: 38 additions & 2 deletions tests/IpToolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,24 @@ public function testIpv6ToCidr()
$ipTools = new \IP2Location\IpTools();

$this->assertEqualsCanonicalizing(
['2002::1234:abcd:ffff:c0a8:101/64'],
$ipTools->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0101', '2002:0000:0000:1234:ffff:ffff:ffff:ffff')
[
'2002::1234:abcd:ffff:c0a8:0/109',
'2002::1234:abcd:ffff:c0b0:0/108',
'2002::1234:abcd:ffff:c0c0:0/106',
'2002::1234:abcd:ffff:c100:0/104',
'2002::1234:abcd:ffff:c200:0/103',
'2002::1234:abcd:ffff:c400:0/102',
'2002::1234:abcd:ffff:c800:0/101',
'2002::1234:abcd:ffff:d000:0/100',
'2002::1234:abcd:ffff:e000:0/99',
'2002:0:0:1234:abce::/79',
'2002:0:0:1234:abd0::/76',
'2002:0:0:1234:abe0::/75',
'2002:0:0:1234:ac00::/70',
'2002:0:0:1234:b000::/68',
'2002:0:0:1234:c000::/66',
],
$ipTools->ipv6ToCidr('2002:0000:0000:1234:abcd:ffff:c0a8:0000', '2002:0000:0000:1234:ffff:ffff:ffff:ffff')
);
}

Expand All @@ -129,4 +145,24 @@ public function testCidrToIpv6()
$ipTools->cidrToIpv6('2002::1234:abcd:ffff:c0a8:101/64')
);
}

public function testCompressIpv6()
{
$ipTools = new \IP2Location\IpTools();

$this->assertEquals(
'2002::1234:ffff:ffff:ffff:ffff',
$ipTools->compressIpv6('2002:0000:0000:1234:ffff:ffff:ffff:ffff')
);
}

public function testExpandIpv6()
{
$ipTools = new \IP2Location\IpTools();

$this->assertEquals(
'2002:0000:0000:1234:ffff:ffff:ffff:ffff',
$ipTools->expandIpv6('2002::1234:ffff:ffff:ffff:ffff')
);
}
}

0 comments on commit 0d83313

Please sign in to comment.