inet_ntop()

(PHP 5 >= 5.1.0, PHP 7)

Converts a packed internet address to a human readable representation

说明inet_ntop(string$in_addr):string

This function converts a 32bit IPv4, or 128bit IPv6 address (if PHP was built with IPv6 support enabled) into an address family appropriate string representation.

参数$in_addrA 32bit IPv4, or 128bit IPv6 address.

返回值

Returns a string representation of the address 或者在失败时返回FALSE.

范例

Example #1inet_ntop()Example<?php

$packed = chr(127) . chr(0) . chr(0) . chr(1);

$expanded = inet_ntop($packed);

/* Outputs: 127.0.0.1 */

echo $expanded;

$packed = str_repeat(chr(0), 15) . chr(1);

$expanded = inet_ntop($packed);

/* Outputs: ::1 */

echo $expanded;

?>

更新日志版本说明5.3.0This function is now available on Windows platforms.

参见For people who wondering what the meaning of this function name:

pton: a presentation(printable) format address to network address

ntop: a network address to presentation(printable) format addressPHP's inet_ntop function is not compatible with the binary representation used by MySQL's INET6_ATON function, assuming you are using the recommended method of storing both IPv4 and IPv6 addresses in a VARBINARY(16) field. You need to convert it like this:

/**

* Convert a MySQL binary v4 (4-byte) or v6 (16-byte) IP address to a printable string.

* @param string $ip A binary string containing an IP address, as returned from MySQL's INET6_ATON function

* @return string Empty if not valid.

*/

function inet6_ntop($ip) {

$l = strlen($ip);

if ($l == 4 or $l == 16) {

return inet_ntop(pack('A' . $l, $ip));

}

return '';

}

You don't need a function going the other way because MySQL's INET6_NTOA is already compatible with PHP's inet_pton function.For people who need this function but don't have it, I could write a function which should give almost the same result.

function my_inet_ntop($ip) {

if (strlen($ip)==4) {

// ipv4

list(,$ip)=unpack('N',$ip);

$ip=long2ip($ip);

} elseif(strlen($ip)==16) {

// ipv6

$ip=bin2hex($ip);

$ip=substr(chunk_split($ip,4,':'),0,-1);

$ip=explode(':',$ip);

$res='';

foreach($ip as $seg) {

while($seg{0}=='0') $seg=substr($seg,1);

if ($seg!='') {

$res.=($res==''?'':':').$seg;

} else {

if (strpos($res,'::')===false) {

if (substr($res,-1)==':') continue;

$res.=':';

continue;

}

$res.=($res==''?'':':').'0';

}

}

$ip=$res;

}

return $ip;

}

?>

I followed IPv6 reprenstation rules :

- A series of "0"s in a 16bit block can by represented by "0".

- A series of blocks containing only "0"s can be suppressed and represented by "::" (this can be done only once)

(source : http://www.ipv6style.jp/en/faq/latest.shtml#2003022112 )

Example:

$packed = str_repeat(chr(0), 15) . chr(1);

$expanded = my_inet_ntop($packed);

/* Outputs: ::1 */

echo $expanded;

?>

I checked in CVS (2005-04-25) and didn't find the inet_ntop function (I really needed to handle IPv6) so I implemented it myself !Before inet_pton nice work

//2001:0db8:85a3:0000:0000:8a2e:0370:7334

After

//2001:db8:85a3::8a2e:370:7334

no need compare compress data

string inet_ntop ( string $in_addr [, bool $compress = false] )

---

inet_ntop php,inet_ntop()相关推荐

  1. IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa

    inet_pton NAME     inet_pton - 将 IPv4 和 IPv6 地址从点分十进制转换为二进制 SYNOPSIS #include <arpa/inet.h>int ...

  2. IP地址转换函数 inet_pton、inet_ntop与 inet_aton、inet_addr、 inet_ntoa

    inet_pton NAME     inet_pton - 将 IPv4 和 IPv6 地址从点分十进制转换为二进制 SYNOPSIS #include <arpa/inet.h>int ...

  3. inet_pton和inet_ntop函数

    Linux下这2个IP地址转换函数,可以在将IP地址在"点分十进制"和"整数"之间转换 而且,inet_pton和inet_ntop这2个函数能够处理ipv4和 ...

  4. 【Socket网络编程】3.字节序转换函数htons、htonl ,地址转换函数inet_ntoa、inet_ntop、inet_pton、inet_addr

    字节序转换函数htons.htonl 地址转换函数inet_ntoa.inet_ntop.inet_pton.inet_addr 1.字节序转换函数 #include <arpa/inet.h& ...

  5. inet_ntop函数和inet_pton函数

    #include<arpa/inet.h> int inet_pton(int family, const char *strptr, void *addrptr);返回:若成功则为1,若 ...

  6. 无法定位程序输入点 inet_ntop 于动态链接库 WS2_32.dll上

    搜索了一下工程里, 根本没有使用到inet_ntop这个函数, 但是debug版运行的时候提示这个错误 无法定位程序输入点 inet_ntop 于动态链接库 WS2_32.dll上 解决方法: 项目- ...

  7. linux网络编程之用一张图片说明函数inet_ntop、inet_pton、inet_addr、inet_ntoa 、inet_aton函数之间的关系

    1.inet_ntop.inet_pton.inet_addr.inet_ntoa .inet_aton函数之间的关系 2.inet_ntop.inet_pton函数的源代码 1.inet_pton函 ...

  8. linux网络编程之inet_pton和inet_ntop函数

    Linux下这2个IP地址转换函数,可以在将IP地址在"点分十进制"和"整数"之间转换 而且,inet_pton和inet_ntop这2个函数能够处理ipv4和 ...

  9. Linux函数--inet_pton / inet_ntop

    http://blog.csdn.net/lindyl/article/details/10427925 inet_pton 和 inet_ntop Linux下这2个IP地址转换函数,可以在将IP地 ...

最新文章

  1. 互联网协议 — 数据路由技术
  2. mysql 8.0.21 安装配置方法图文教程
  3. Git CMD - show: Show various types of objects
  4. SQLServer导入Excel截断数据的解决办法
  5. linux查看ibmmq进程,ibm-mq – 如何检查IBM MQ for Linux中是否存在队列名称?
  6. jQuery和Vue的区别(转载)
  7. (飞鸽传书绿色版)网站
  8. linkerd mysql_linkerd ab部署测试
  9. 怎么判断间隙过渡过盈配合_圆柱销有2个标准,选型注意材料和热处理,特别是销孔的配合关系...
  10. matlab 数字基带,基于MATLAB的数字基带传输的 FIR滤波器的设计
  11. Atitit 设计模式与算法,与流程的关系
  12. IOS移动应用跳转微信小程序
  13. STM32F103ZET6+红外温度传感器mlx90614芯片
  14. 没有项目经验找工作处处碰壁怎么办
  15. 【Unity】UI或3D场景自动设置渐变色背景
  16. BugkuCTF 部分题解(随缘更新)
  17. 嵌入式开发对学历门槛要求高吗?
  18. 【C语言实用库】cJSON-解析json数据的利器
  19. 软件危机产生的原因与表现及解决方法
  20. human3.6源文件处理 简单解析

热门文章

  1. mysqldump命令详解(转载)
  2. GlusterFS-FUSE
  3. REDIS 高可用性部署架构图
  4. 快速搭建samba服务
  5. tornado简单实现restful接口2
  6. 入口文件到控制器 php,workerman的入口文件_PHP开发框架教程
  7. linux mysql cpu 查看工具_Shell编程检测监控MySQL的CPU占用率
  8. python中options类_python中的optionParser模块
  9. Linux启动屏幕打印日志,linux启动时如何在屏幕上找到文本显示?这...
  10. 使用ESP32 MicroPython I2C功能读取 BH1750光度传感器模块数据