-1

I'm trying to generate a range of ips using php (CLI)

But i want to remove " [value] => "

And show only the generated range of ips

The code i am using :

function ip_range($start, $end) {  

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

echo "start of ip range :";
$range_one= trim(fgets(STDIN,1024));
echo "End of ip range";
$range_two= trim(fgets(STDIN,1024));
$icodz =ip_range($range_one, $range_two);
print_r($icodz);  

Output of this code is :

[0] => 192.168.1.0
[1] => 192.168.1.1
[2] => 192.168.1.2
[3] => 192.168.1.3
[4] => 192.168.1.4
[5] => 192.168.1.5
[6] => 192.168.1.6
[7] => 192.168.1.7
[8] => 192.168.1.8
[9] => 192.168.1.9
[10] => 192.168.1.10
[11] => 192.168.1.11
[12] => 192.168.1.12

The output I want is :

     192.168.1.0
     192.168.1.1
     192.168.1.2
     192.168.1.3
     192.168.1.4
     192.168.1.5
     192.168.1.6
     192.168.1.7
     192.168.1.8
     192.168.1.9
     192.168.1.10
     192.168.1.11
     192.168.1.12

Any solution?

1stthomas
  • 731
  • 2
  • 15
  • 22

2 Answers2

0

This is a very basic thing. Look up tutorials on how to loop through arrays in PHP.

function ip_range($start, $end) {  
  $start = ip2long($start);  
  $end = ip2long($end);  
  return array_map('long2ip', range($start, $end) );
   }
   echo "start of ip range :";
   $range_one= trim(fgets(STDIN,1024));
   echo "End of ip range";
   $range_two= trim(fgets(STDIN,1024));
   $icodz =ip_range($range_one, $range_two);
   //print_r($icodz); 

   foreach($icodz as $ips) {
    echo $ips."<br>";
   }

Break down

   foreach($icodz as $ips) { // For every IP in the array

    echo $ips."<br>"; // Output the IP plus a break/new line to make it a list

   }
ProEvilz
  • 5,310
  • 9
  • 44
  • 74
  • Whenever you see something that you think is a very basic thing, chances are the answer already exists on SO, and the right thing for you to do is to go find the duplicate and vote to close as duplicate, NOT to answer the question. – random_user_name Jan 04 '18 at 17:28
  • i'm sorry i searched and i didnt find the solution so i asked here – mohamed soufellou Jan 04 '18 at 17:30
-1

replace print_r( $icodz ) with this :

foreach($icodz as $key => $value) 
{
    print($value . '<br />');
}
azjezz
  • 3,827
  • 1
  • 14
  • 35