Php convert ipv6 to number

前端 未结 7 880
醉话见心
醉话见心 2021-01-31 20:53

In Ipv4 we can use ip2long to convert it to number,

How to convert ipv6 compressed to number in PHP?

I tried inet_pton and it\'s not working.

<
7条回答
  •  抹茶落季
    2021-01-31 21:17

    Try this

    function inet6_to_int64($addr)
    {
        /* Expand the address if necessary */
        if (strlen($addr) != 39) {
            $addr = inet6_expand($addr);
            if ($addr == false) return false;
        } // if
        $addr = str_replace(':', '', $addr);
        $p1 = '0x' . substr($addr, 0, 16);
        $p2 = '0x' . substr($addr, 16);
        $p1 = gmp_init($p1);
        $p2 = gmp_init($p2);
        $result = array(gmp_strval($p1), gmp_strval($p2));
        return $result;
    } // inet6_to_int64()
    

    For more functions or details please visit http://www.soucy.org/project/inet6/

提交回复
热议问题