kickassamd Posted October 19, 2010 Share Posted October 19, 2010 This works if say I do 192.168.0.1 - 192.168.1.254 it will work. However doing 192.168.0.1 - 192.168.1.10 will only generate up to .10 on both IP schemes. I see why, but I cannot figure out a way around this... Thanks! $start = explode('.', '192.168.10.1'); $end = explode('.', '192.168.11.10'); /** * Final output... */ $a = 0; $b = 0; $c = 0; $d = 0; // First segment for ($a = $start[0]; $a < 1 + $end[0]; $a++) { // Second segment for ($b = $start[1]; $b < 1 + $end[1]; $b++) { // Third segment for ($c = $start[2]; $c < 1 + $end[2]; $c++) { // Fourth segment for ($d = $start[3]; $d < 1 + $end[3]; $d++) { echo "{$a}.{$b}.{$c}.{$d}<br />"; } } } } Quote Link to comment Share on other sites More sharing options...
cigardude Posted October 19, 2010 Share Posted October 19, 2010 I would check out ip2long and long2ip. This way you can just deal with increasing a long and converting with the functions. http://us2.php.net/manual/en/function.ip2long.php Quote Link to comment Share on other sites More sharing options...
kickassamd Posted October 19, 2010 Author Share Posted October 19, 2010 Thank you! I was looking for this function and boom it hit me! Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2010 Share Posted October 19, 2010 For some reason this creates an infinite loop when I try to increment $decimal using $decimal++, so I just incremented it long hand. $startIP = '192.168.10.1'; $endIP = '192.168.11.10'; $startDecimal = sprintf("%u\n", ip2long($startIP)); $endDecimal = sprintf("%u\n", ip2long($endIP)); for($decimal=$startDecimal; $decimal<=$endDecimal; $decimal=$decimal+1) { echo long2ip($decimal) . "<br />\n"; } Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 19, 2010 Share Posted October 19, 2010 For some reason this creates an infinite loop when I try to increment $decimal using $decimal++ that is baffling. why does that happen? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 19, 2010 Share Posted October 19, 2010 For some reason this creates an infinite loop when I try to increment $decimal using $decimal++ that is baffling. why does that happen? var_dump($decimal+1); var_dump($decimal++); var_dump(++$decimal); Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 19, 2010 Share Posted October 19, 2010 so, it is the result of sprintf giving us a string? Quote Link to comment Share on other sites More sharing options...
Fligex Posted October 19, 2010 Share Posted October 19, 2010 I took the liberty to write out a script to perform the task stated by the OP... I don't have time to fin it atm but you can take a look and get an idea of how i'm doing it, possibly help you.. I'll try to finish it after my next class. <?php ///////////////////////////////////////// ///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/// ///^^ Calculate Ip Range difference ^^/// ///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/// ///////////////////////////////////////// //Example (192.168.11.10) - (192.168.10.1) = 268 Addresses //////////////////////////////// //### START AND END IP #s ###/// //////////////////////////////// $Start = explode('.','192.168.10.1'); $End = explode('.','192.168.11.10); $Part =; //////////////////////////////////////////////////// //### Initiate Place holder for generated IP's ###// //////////////////////////////////////////////////// $GendIP = Array(); /////////////////////////////// //### Min/Max IP # Ranges ###// /////////////////////////////// $Max = 255; $Min = 0; $Cap = Array(); $NumIP = 0; ////////////////////////// //### How Many IP's? ###// ////////////////////////// for($i=0; $i<4; $i++) { //First check if the IP segments match //as then there is not calc to be done if($Start[$i] == $End[$i]) { //The first segment of the two IP's given match //So there is nothing to be done. //Set cap place holder $Cap[$i] = nil; } else { //The IP segments don't match so we will find a //range cap for this segment $Cap[$i] = $End[$i] - $Start[$i]; } if($Cap[$i] != 'nil') { $NumIP += $Cap[$i] } } //Gen the Ip's for($i=0; $i<=$NumIP; $i++) { } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 19, 2010 Share Posted October 19, 2010 i was able to make the "regular" incrementor work by forcing the start/end values to floats: $startDecimal = floatval(sprintf("%u", ip2long($startIP))); $endDecimal = floatval(sprintf("%u", ip2long($endIP))); Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 20, 2010 Share Posted October 20, 2010 I took the liberty to write out a script to perform the task stated by the OP... I don't have time to fin it atm but you can take a look and get an idea of how i'm doing it, possibly help you.. I'll try to finish it after my next class. I guess reading the other responses was too much work for you? I already provided a working solution that was more efficient than what you posted later. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 I wouldn't use sprintf() at all: $startIP = '192.168.10.1'; $endIP = '192.168.11.10'; $start = ip2long($startIP); $end = ip2long($endIP); for($d=$start; $d<=$end; $d++) { echo long2ip($d) . "<br />\n"; } Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 20, 2010 Share Posted October 20, 2010 as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 20, 2010 Share Posted October 20, 2010 I wouldn't use sprintf() at all: as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to. Heaven forbid you actually spend a few precious moments of your time to find out WHY I used it. You might actually learn something. I don't typically use it either. But, if you were to read the manual for ip2long() you would know that there is a valid reason to use sprintf() with the %u formatter! From the manual (emphasis added) Note: Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 I wouldn't use sprintf() at all: as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to. Heaven forbid you actually spend a few precious moments of your time to find out WHY I used it. You might actually learn something. I don't typically use it either. But, if you were to read the manual for ip2long() you would know that there is a valid reason to use sprintf() with the %u formatter! From the manual (emphasis added) Note: Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address. Yes, but in this case we don't need a string representation of an unsigned integer. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 20, 2010 Share Posted October 20, 2010 I wouldn't use sprintf() at all: as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to. Heaven forbid you actually spend a few precious moments of your time to find out WHY I used it. You might actually learn something. I don't typically use it either. But, if you were to read the manual for ip2long() you would know that there is a valid reason to use sprintf() with the %u formatter! From the manual (emphasis added) Note: Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address. Yes, but in this case we don't need a string representation of an unsigned integer. The fact that sprintf returns a string is irrelevant. The fact is that you need the return value from ip2long as an unsigned integer in order to return the correct address when you pass it to long2ip. u - the argument is treated as an integer, and presented as an unsigned decimal number. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 The fact that sprintf() returns a string is irrelevant. No, the original problem was that using the post increment operator on the string was causing problems. The fact is that in this case, an unsigned number string is not needed. The fact is that you need the return value from ip2long() as an unsigned integer in order to return the correct address when you pass it to long2ip(). If possible, please post an example/test case of when using ip2long() and then long2ip() without using sprintf() to return a string representation of an unsigned number will give incorrect results. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.