Skip to content

Instantly share code, notes, and snippets.

@Dragory
Last active August 29, 2015 14:05
Show Gist options
  • Save Dragory/d5671d6887b9c70348d5 to your computer and use it in GitHub Desktop.
Save Dragory/d5671d6887b9c70348d5 to your computer and use it in GitHub Desktop.
<?php
$startIP = [1, 1, 1, 50];
$numToGenerate = 255;
// Convert the array into the binary representation
$startIP = array_map(function($num) {
return str_pad(decbin($num), 8, "0", STR_PAD_LEFT);
}, $startIP);
$startIP = implode('', $startIP); // Should be 32 bits represented as binary here (in a string)
// Generate the IPs
$startIPDec = bindec($startIP);
$results = [];
for ($i = 0; $i < $numToGenerate; $i++) {
// Save each IP as the 32 bit binary string
$results[] = str_pad(decbin($startIPDec + $i), 32, "0", STR_PAD_LEFT);
}
// Output
foreach ($results as $result) {
echo implode('.', array_map('bindec', str_split($result, 8)));
echo '<br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment