Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,将IP地址数组中的数字增加1_Php_Arrays_Math - Fatal编程技术网

在PHP中,将IP地址数组中的数字增加1

在PHP中,将IP地址数组中的数字增加1,php,arrays,math,Php,Arrays,Math,我想将数组+1的数字添加到每个IP地址。我知道bcadd;但我不能让它工作,因为每个IP地址都有不同的长度,我只需要增加它的最后数量 例如: array("194.32.14.152", "4.189.23.35", etc...); 。。。将成为: array("194.32.14.153", "4.189.23.36", etc...); 现在也许我需要使用str_pad;匹配最后一个点 任何帮助都将不胜感激。您可以将阵列映射到新的IP地址。在map方法中,可以按拆分当前字符串。使用爆

我想将数组+1的数字添加到每个IP地址。我知道bcadd;但我不能让它工作,因为每个IP地址都有不同的长度,我只需要增加它的最后数量

例如:

array("194.32.14.152", "4.189.23.35", etc...);
。。。将成为:

array("194.32.14.153", "4.189.23.36", etc...); 
现在也许我需要使用str_pad;匹配最后一个点


任何帮助都将不胜感激。

您可以将阵列映射到新的IP地址。在map方法中,可以按拆分当前字符串。使用爆炸。然后,要从IP获取最后一个数字,可以使用array_pop,然后可以将其转换为整数,以便向其中添加一个。然后,您可以将更新后的值推送到部件数组中,并使用内爆将数组中的每个部件重新连接在一起

见下例:

$arr = array("194.32.14.152", "4.189.23.35", "4.189.23.255");

$res = array_map(function($v) { // Example, let $v = "194.32.14.152";
  $parts = explode('.', $v); // "194.32.14.152" -> ["194", "32", "14", "152"];
  array_push($parts, min((int) array_pop($parts)+1, 255)); // ["194", "32", "14", 153]
  return implode('.', $parts); // "194.32.14.153"
}, $arr);

print_r($res); // ["194.32.14.153", "4.189.23.36", "4.189.23.255"]
IMO的preg_replace_回调是最简洁和合适的方法。如果只有一个本机函数可以完成所有任务,那么为什么还要做其他事情呢

匹配最终的数字序列,只要不是255,就增加子字符串

代码:

模式:

\d+        #match 1 or more digits
$          #match the end of the string
(?<!255)   #lookbehind to ensure the matched number is not literally 255

在使用此模式时,您不必费心处理255,而是增加所有其他匹配的数字。

另一个使用内置和PHP的解决方案


现场演示

如果一个IP看起来像194.32.14.159,会发生什么?它会变成194.32.14.160等等,这并不重要,因为最后一部分中的每个数字都应该增加+1。如果你有192.32.14.255,并且加1,会发生什么情况?@MarkusZeller此时,它将变成。但是你提出了一个很好的观点。当OP看到我建议处理该案件的答案时,我会看看他们是否希望处理该案件。否则答案是完全错误的,即使它是有效的。这实际上非常有效@Nick Parsons感谢您的这种方法,我们将学习这种方法,并了解更多您是如何做到这一点的。@新手您是否担心IP超过255的限制?从技术上讲,192.32.14.256不是valid@Newbie这是一种更干净的方式。如果您需要进一步解释,请询问。谢谢您的这种方法。遗憾的是,我仍然没有学习preg_replace语法,所以第一个解决方案似乎更容易理解。@mickmackusa谢谢。我想将其扩展到IPv6,但IPv6没有ip2long和long2ip:
var_export(preg_replace_callback('~\d+$(?<!255)~', fn($m) => ++$m[0], $ips));
array (
  0 => '194.32.14.200',
  1 => '4.189.23.36',
  2 => '4.189.23.255',
)
\d+        #match 1 or more digits
$          #match the end of the string
(?<!255)   #lookbehind to ensure the matched number is not literally 255
<?php

$ips = array("194.32.14.152", "4.189.23.35", "4.51.11.255");
$newIps = [];
foreach($ips as $ipString){
    $ip = ip2long($ipString);
    $lastByte = ($ip & 0x000000FF)+1;
    $lastByte = $lastByte > 255 ? 255 : $lastByte;
    $newIps[]= long2ip(( $lastByte ) | (0xFFFFFF00 & $ip ) );
}
var_dump($newIps);
array(3) {
  [0]=>
  string(13) "194.32.14.153"
  [1]=>
  string(11) "4.189.23.36"
  [2]=>
  string(11) "4.51.11.255"
}