使用PHP扩展IPv6地址的快速方法 [英] Quick way of expanding IPv6 Addresses with PHP

查看:155
本文介绍了使用PHP扩展IPv6地址的快速方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个需要扩展IPv6地址的项目中工作.其他用户创建的功能很少,而现有的功能很难看.其中一些包含多个foreachgmp_init,这增加了很多开销并且更难维护代码.我需要一个简单的,不费力的脚本来扩展IPv6.

I was working on a project where I needed to expand IPv6 addresses. There are not many functions out there created by other users, and the ones that exist are ugly. Some of them included multiple foreach's and gmp_init, which added a lot of overhead and harder to maintain code. I need a simple, non-taxing script to expand IPv6.

为此发布到社区.

Posting this for the community.

推荐答案

以下是两个衬里,其中$ip是压缩的IPv6地址.返回扩展的$ip.

The following is a two liner, where $ip is a condensed IPv6 address. Returns expanded $ip.

示例:

$ip = "fe80:01::af0";
echo expand($ip); // fe80:0001:0000:0000:0000:0000:0000:0af0

功能:

function expand($ip){
    $hex = unpack("H*hex", inet_pton($ip));         
    $ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);

    return $ip;
}

这篇关于使用PHP扩展IPv6地址的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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