It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
6年前关闭。
我使用的是ip2long函数,它工作正常,返回数组中的一长串IP地址。
我想做的是把每个ip地址放到一个mysql数据库中,我已经看了几个例子,但是找不到任何与ip2long函数相关的例子,如果我诚实的话,他们只是把我弄糊涂了。
下面是我的代码,
function ip_range($start, $end) {
$start = ip2long($start);
$end = ip2long($end);
return array_map('long2ip', range($start, $end) );
}

$range_one = "151.76.0.0";
$range_two = "151.76.255.255";
print_r( ip_range($range_one, $range_two) );

如有任何建议,将不胜感激。
谢谢

最佳答案

你的函数看起来很好,产生了很多ip地址。。。

function ip_range($start, $end) {
    $start = ip2long($start);
    $end = ip2long($end);
    return array_map('long2ip', range($start, $end) );
}

$range = ip_range("151.76.0.0", "151.76.255.255");

但是我仍然不确定你想用什么方式存储IP,每一个IP都在不同的行中,还是应该在一行中?
假设每个ip在一个单独的行中,只需在阵列上循环:
foreach ($range as $ip) {
    // create db entry for $id
    // id column should be of type varchar(15)
}

如果整个ip范围只有一行:
$ip = implode(',', $range);
// create db entry for $id

您应该使用列类型文本,因为这些范围可能会变得相当长。
在这两种情况下都浪费了大量的数据空间,为什么不在数据库中存储ip范围的长版本呢?
四列id,ip_start aslong,ip_end aslong,netname;然后在查询ip地址时,只需先查找网络$ip = ip2long(...)

关于php - 将ip2long数组变成单个对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15454910/

10-13 07:08