无法让 ip2long() 工作
Can't get ip2long() to work
我正在尝试比较一些存储为字符串的 IP 地址。我读到有关 ip2long()
将字符串转换为整数的信息,所以我正在研究它,看看它是否能满足我的要求。所以,最初,我只是想看看转换后的 IP 长什么样。我写了下面的代码来查看,但没有任何内容被转换。我不明白我做错了什么。这是代码:
<?php
foreach($form->data[vicondb] as $video_net_key)
{
if ($video_net_key['ipaddress'] != null)
{
echo "The IP address is " . $video_net_key['ipaddress'];
$iplong = ip2long($video_net_key['ipaddress']);
echo " --- The ip long conversion is " . $iplong . "<br>";
}
} // End foreach($form->data[vicondb] as $video_net_key)
echo "End of both foreach statements<br>";
?>
这是结果示例:
The IP address is 172.26.0.10 --- The ip long conversion is
The IP address is 172.26.0.31 --- The ip long conversion is
The IP address is 172.26.0.32 --- The ip long conversion is
The IP address is 172.26.0.33 --- The ip long conversion is
这里是 IP 的 var_dump()
示例并转换为整数:
string(12) "172.26.0.10 "
bool(false)
string(12) "172.26.0.31 "
bool(false)
string(12) "172.26.0.32 "
bool(false)
可能类似这样
"Because PHP's integer type is signed, and many IP addresses will result in negative integers on 32-bit architectures, you need to use the "%u" sprintf() 或 printf() 的格式化程序以获取未签名 IP 地址的字符串表示形式。" From ip2long php.net article
这应该有效:
echo " --- The ip long conversion is " . sprintf("%u", ip2long($video_net_key['ipaddress'])) . "<br>";
您需要 trim IP,因为它似乎有尾随 space 并且无效:
$iplong = ip2long(trim($video_net_key['ipaddress']));
我正在尝试比较一些存储为字符串的 IP 地址。我读到有关 ip2long()
将字符串转换为整数的信息,所以我正在研究它,看看它是否能满足我的要求。所以,最初,我只是想看看转换后的 IP 长什么样。我写了下面的代码来查看,但没有任何内容被转换。我不明白我做错了什么。这是代码:
<?php
foreach($form->data[vicondb] as $video_net_key)
{
if ($video_net_key['ipaddress'] != null)
{
echo "The IP address is " . $video_net_key['ipaddress'];
$iplong = ip2long($video_net_key['ipaddress']);
echo " --- The ip long conversion is " . $iplong . "<br>";
}
} // End foreach($form->data[vicondb] as $video_net_key)
echo "End of both foreach statements<br>";
?>
这是结果示例:
The IP address is 172.26.0.10 --- The ip long conversion is
The IP address is 172.26.0.31 --- The ip long conversion is
The IP address is 172.26.0.32 --- The ip long conversion is
The IP address is 172.26.0.33 --- The ip long conversion is
这里是 IP 的 var_dump()
示例并转换为整数:
string(12) "172.26.0.10 "
bool(false)
string(12) "172.26.0.31 "
bool(false)
string(12) "172.26.0.32 "
bool(false)
可能类似这样
"Because PHP's integer type is signed, and many IP addresses will result in negative integers on 32-bit architectures, you need to use the "%u" sprintf() 或 printf() 的格式化程序以获取未签名 IP 地址的字符串表示形式。" From ip2long php.net article
这应该有效:
echo " --- The ip long conversion is " . sprintf("%u", ip2long($video_net_key['ipaddress'])) . "<br>";
您需要 trim IP,因为它似乎有尾随 space 并且无效:
$iplong = ip2long(trim($video_net_key['ipaddress']));