Fast way to extend IPv6 addresses with PHP

I was working on a project where I needed to extend IPv6 addresses. There are not many functions created by other users, and those that exist are ugly. Some of them included several foreach and gmp_init , which added a lot of overhead and more difficult to maintain the code. I need a simple, tax-free script to extend IPv6.

Posting for the community.

+4
php tcp ipv6
Aug 23 2018-12-12T00:
source share
1 answer

The two lines below are where $ip is the compressed IPv6 address. Returns the extended $ip .

An example :

 $ip = "fe80:01::af0"; echo expand($ip); // fe80:0001:0000:0000:0000:0000:0000:0af0 

Function

 function expand($ip){ $hex = unpack("H*hex", inet_pton($ip)); $ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1); return $ip; } 
+16
Aug 23 2018-12-12T00:
source share



All Articles