Change IPv4 to IPv6 string

后端 未结 3 1331
被撕碎了的回忆
被撕碎了的回忆 2021-01-12 17:33

Sander Steffann mentioned in a previous question of mine:

Addresses like 0000:0000:0000:0000:0000:0000:192.168.0.1 are written as 0000:0000:0000:00

3条回答
  •  無奈伤痛
    2021-01-12 17:47

    Your best bet is to not do this manually, but instead call inet_pton to get a binary representation, and then convert that to the format you wish to have.

    $foo = inet_pton("::1");
    for ($i = 0 ; $i < 8 ; $i++)
        $arr[$i] = sprintf("%02x%02x", ord($foo[$i * 2]), ord($foo[$i * 2 + 1]));
    $addr = implode(":", $arr);
    

提交回复
热议问题