Mac Ben Mac Ben - 5 months ago 13
PHP Question

Transform ip address to ip range (Start IP - End IP)

I am looking for a function that give me this result:

127.0.0.0-127.0.0.255


With only this ip:

127.0.0.1 or 127.0.0.50 ...


Function will look like:

function transform_ip($ip) {
// Method to transform ip the ip-range
// Specify Start IP and End IP
return $result;
}
echo transform_ip("127.0.0.66"); // result: 127.0.0.0-127.0.0.255

Answer

There are hundreds of ways to trim off everything after the last period. One way is to explode and implode without the last item...

$a = explode('.',$ip); // $a is each number without the periods
pop($a); // Pop off the last number
$ip = implode('.',$a); // Recombine with periods

Now, you can just return your string:

$result = "$ip.0-$ip.255";