Search code examples
phpbinaryipv6ipv4

Convert IPv6 to IPV4 PHP


I have a list of IPv4 IPs selected from a database which stores the addresses as BINARY(16). Is there any simple way to convert the IPv6 formated address to human readable IPv4 format? This is what the IPv4 address looks like 8ab8:7f70::


Solution

  • As Ron Maupin described, the solution is very simple

    $ipv6 = "8ab8:7f70::";
    $ipv4 = hexdec(substr($ipv6, 0, 2)). "." . hexdec(substr($ipv6, 2, 2)). "." . hexdec(substr($ipv6, 5, 2)). "." . hexdec(substr($ipv6, 7, 2));