-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?

3
  • ok thank you i'm gonna delete it because i found the solution Commented Jan 4, 2018 at 17:31
  • + isearched a lot and i didnt find the solution so iasked here Commented Jan 4, 2018 at 17:31
  • Instead of making another answer I post it here. Don't use the below methods. Arrays can be imploded. echo implode("<br>\n", $icodz); will add br and new line between each value in the array and return it as string. Commented Jan 4, 2018 at 17:33

2 Answers 2

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

   }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
i'm sorry i searched and i didnt find the solution so i asked here
-1

replace print_r( $icodz ) with this :

foreach($icodz as $key => $value) 
{
    print($value . '<br />');
}

2 Comments

This will output all IP's inline instead of a list.
it shall work now, if you are using this script on cli replace '<br>' with "\n"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.