在PHP的replacement alternative for inet_pton()中,给出了以下代码:
<?php
function inet_pton($ip)
{
# ipv4
if (strpos($ip, '.') !== FALSE) {
$ip = pack('N',ip2long($ip));
}
# ipv6
elseif (strpos($ip, ':') !== FALSE) {
$ip = explode(':', $ip);
$res = str_pad('', (4*(8-count($ip))), '0000', STR_PAD_LEFT);
foreach ($ip as $seg) {
$res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
}
$ip = pack('H'.strlen($res), $res);
}
return $ip;
}
?>
但是,当使用以下测试代码对其进行测试时,它表明并非所有条目都是正确的:
<?php
$arrIPs = array(
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"fe80:01::af0",
"::af0",
"192.168.0.1",
"0000:0000:0000:0000:0000:0000:192.168.0.1");
foreach($arrIPs as $strIP) {
$strResult = bin2hex(inet_pton($strIP));
echo "From: {$strIP} to: {$strResult}<br />\n";
}
/*
From: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 to: 20010db885a3000000008a2e03707334
From: fe80:01::af0 to: 0000000000000000fe80000100000af0 //Incorrect
From: ::af0 to: 00000000000000000000000000000af0
From: 192.168.0.1 to: c0a80001
From: 0000:0000:0000:0000:0000:0000:192.168.0.1 to: 00000000 //Incorrect
*/
?>
我不知道正确的IPv6语法,所以我更喜欢其他人,他们更了解IPv6和标准,看看这个并告诉我它有什么问题?
答案 0 :(得分:4)
我刚刚从php.net下载了PHP 5.3安装程序,安装程序中包含了PEAR。它默认情况下没有安装:
答案 1 :(得分:3)
此代码将正确执行:
function inet_pton($ip){
# ipv4
if (strpos($ip, '.') !== FALSE) {
if (strpos($ip, ':') === FALSE) $ip = pack('N',ip2long($ip));
else {
$ip = explode(':',$ip);
$ip = pack('N',ip2long($ip[count($ip)-1]));
}
}
# ipv6
elseif (strpos($ip, ':') !== FALSE) {
$ip = explode(':', $ip);
$parts=8-count($ip);
$res='';$replaced=0;
foreach ($ip as $seg) {
if ($seg!='') $res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
elseif ($replaced==0) {
for ($i=0;$i<=$parts;$i++) $res.='0000';
$replaced=1;
} elseif ($replaced==1) $res.='0000';
}
$ip = pack('H'.strlen($res), $res);
}
return $ip;
}
结果:
From: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 to:
string '20010db885a3000000008a2e03707334' (length=32)
From: fe80:01::af0 to:
string 'fe800001000000000000000000000af0' (length=32)
From: ::af0 to:
string '00000000000000000000000000000af0' (length=32)
From: 192.168.0.1 to:
string 'c0a80001' (length=8)
From: 0000:0000:0000:0000:0000:0000:192.168.0.1 to:
string 'c0a80001' (length=8)