本文介紹了使用 PHP 擴展 IPv6 地址的快速方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在從事一個需要擴展 IPv6 地址的項目.其他用戶創建的功能并不多,而且存在的功能都很丑陋.其中一些包括多個 foreach
和 gmp_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.
為社區發布此內容.
推薦答案
以下是兩行,其中 $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 地址的快速方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!