Reputation: 1
i have text file contains IP range like this format ::
52.0.0.0-52.1.255.255
52.5.0.0-52.50.255.255
i want to Generate IP list from this range , line by line i try to do that by using this code but its not working .
<?php
$file = file('ips.txt');
foreach ($file AS $line) {
$ips = explode('-', $line);
$range_one = $ips[0];
$range_two = $ips[1];
$ip1 = ip2long ($range_one);
$ip2 = ip2long ($range_two);
while ($ip1 <= $ip2) {
print_r (long2ip($ip1) ."". "\n");
$ip1 ++;
}
}
?>
and i want to save the output at txt file , Please help to do that and correct the mistake if i have .
Upvotes: 0
Views: 299
Reputation: 544
Add code to remove \r\n or \n in each line be for explode '-'
And also save output to text file.
<?php
$file = file('ips.txt');
$data = "";
foreach ($file AS $line) {
$ip_range = str_replace(array("\r\n","\r"),"",$line); /* remove \r\n or \n before explode '-' */
$ips = explode('-', $ip_range);
$range_one = $ips[0];
$range_two = $ips[1];
$ip1 = ip2long ($range_one);
$ip2 = ip2long ($range_two);
while ($ip1 <= $ip2) {
$data .= (long2ip($ip1) ."". "\n"); /* save each line to string */
$ip1 ++;
}
}
print_r ($data);
file_put_contents("ips_list.txt", $data); /* save output to text file */
?>
Upvotes: 1