对不起,如果之前有人问过这个问题,但我的搜索没有太大帮助。我在脚本中合并 PHP 内置函数"ip2long"时遇到了一些问题。输入 IP 地址来自文本文件,该函数仅选取列表中的最后一个地址进行转换。我正在 32 位机器上运行脚本,如果这提供了问题的线索。
<?php
$text_file = 'ip.txt';
//begin reading text file
if (($file = fopen($text_file, "r")) !== FALSE) {
echo "Begin IP Convertion n";
while ($line = fgets($file)) {
echo "$line = " . ip2long($line) . "n";
}
} else {echo "No file found n";}
fclose($file);
?>
以下是 ip.txt 文件的内容:
127.0.0.1
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
运行脚本时的结果:
# php ip.php
Begin IP Convertion
127.0.0.1
=
1.1.1.1
=
2.2.2.2
=
3.3.3.3
=
4.4.4.4 = 67372036
#
-乔恩
您必须使用类似 trim
的内容从字符串中删除换行
<?php
$text_file = 'ip.txt';
//begin reading text file
if (($file = fopen($text_file, "r")) !== FALSE) {
echo "Begin IP Convertion n";
while ($line = fgets($file)) {
echo trim($line) . " = " . ip2long(trim($line)) . "n";
}
} else {echo "No file found n";}
fclose($file);
?>