将IPv4更改为IPv6字符串 [英] Change IPv4 to IPv6 string

查看:171
本文介绍了将IPv4更改为IPv6字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像0000:0000:0000:0000:0000:0000:192.168.0.1这样的地址写为 0000:0000:0000:0000:0000:0000:0000:c0a8:0001这是完全相同的地址 但以十六进制表示.

Addresses like 0000:0000:0000:0000:0000:0000:192.168.0.1 are written as 0000:0000:0000:0000:0000:0000:c0a8:0001 which is exactly the same address but in hex notation.

如何在PHP中检测是否写入了地址,例如:::0000:192.168.0.10000::0000:192.168.0.10000:0000:0000:0000:0000:0000:192.168.0.1等?检查基于IP的字符串是否具有."是否足够? AND':'吗?

How do I detect in PHP if an address was written like eg.: ::0000:192.168.0.1 or 0000::0000:192.168.0.1 or 0000:0000:0000:0000:0000:0000:192.168.0.1 etc.? Is it enough to check if an IP-based string has '.' AND ':' ?

如何将其更改为完整字符串0000:0000:0000:0000:0000:0000:c0a8:0001?

And how do I change this to the full string 0000:0000:0000:0000:0000:0000:c0a8:0001?

我是正确的,将其更改为IPv4将类似于:

Am I correct, to change this to IPv4 will be something like:

<?php 
$strIP = '0000:0000:0000:0000:0000:0000:192.168.0.1';

$strResult = substr($strIP, strrpos($strIP, ':'));

echo $strResult; //192.168.0.1 ?
?>

...还是正确的IP字符串表示形式比此代码片段所能做到的复杂?

... or are correct IP string representations more complex than what this snippet could do?

推荐答案

首先:您为什么要关心地址的写法? inet_pton()将为您解析所有变体,并为您提供一致的结果,然后您可以将其转换为二进制,十六进制或任何您想要的形式.

First of all: why would you care how the address is written? inet_pton() will parse all variations for you and give you a consistent result, which you can then transform into binary, hex, or whatever you want.

所有将类似::192.168.0.1转换为0000:0000:0000:0000:0000:0000:c0a8:0001之类的代码实际上都在我的帖子中.这正是我的示例函数所做的.

All the code for converting things like ::192.168.0.1 to 0000:0000:0000:0000:0000:0000:c0a8:0001 was actually in my post. That's exactly what my example function does.

如果将0000:0000:0000:0000:0000:0000:192.168.0.1馈送到inet_pton()然后又馈给inet_ntop(),您将获得规范的IPv6表示法,在这种情况下为::192.168.0.1.如果该字符串以::开头,其余部分不包含:和三个点,那么您可以确定它是一个IPv4地址;-)

If you feed 0000:0000:0000:0000:0000:0000:192.168.0.1 to inet_pton() and then to inet_ntop() you'll get the canonical IPv6 notation, which is ::192.168.0.1 in this case. If that string begins with :: and the rest contains no : and three dots then you can be pretty sure it's an IPv4 address ;-)

要将先前问题的答案与此问题结合在一起:

To combine the answer to your previous question with this question:

function expand_ip_address($addr_str) {
  /* First convert to binary, which also does syntax checking */
  $addr_bin = @inet_pton($addr_str);
  if ($addr_bin === FALSE) {
    return FALSE;
  }

  $addr_hex = bin2hex($addr_bin);

  /* See if this is an IPv4-Compatible IPv6 address (deprecated) or an
     IPv4-Mapped IPv6 Address (used when IPv4 connections are mapped to
     an IPv6 sockets and convert it to a normal IPv4 address */
  if (strlen($addr_bin) == 16
  &&  substr($addr_hex, 0, 20) == str_repeat('0', 20)) {
    /* First 80 bits are zero: now see if bits 81-96 are either all 0 or all 1 */
    if (substr($addr_hex, 20, 4) == '0000')
    ||  substr($addr_hex, 20, 4) == 'ffff')) {
      /* Remove leading bits so only the IPv4 bits remain */
      $addr_bin = substr($addr_hex, 12);
    }
  }

  /* Then differentiate between IPv4 and IPv6 */
  if (strlen($addr_bin) == 4) {
    /* IPv4: print each byte as 3 digits and add dots between them */
    $ipv4_bytes = str_split($addr_bin);
    $ipv4_ints = array_map('ord', $ipv4_bytes);
    return vsprintf('%03d.%03d.%03d.%03d', $ipv4_ints);
  } else {
    /* IPv6: print as hex and add colons between each group of 4 hex digits */
    return implode(':', str_split($addr_hex, 4));
  }
}

这篇关于将IPv4更改为IPv6字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆