-5

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
Mac Ben
  • 43
  • 1
  • 8

2 Answers2

2

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";
kainaw
  • 4,256
  • 1
  • 18
  • 38
0

Your question does not make much sense because if you want to get real range of the IPs in the network you would need to give second parameter which is IP mask.

So when you have IP with class C it has mask 255.255.255.0 and ip 192.168.0.1

when you have IP and mask you can calculate broadcast address and network address. In this particualr case it would be:

Network 192.168.0.0 which is network adress and 192.168.0.255 which is broadcast address, the adress pool would be 192.168.0.1 - 192.168.0.254

For example at following link you can find function how to do it

Also there are functions that convert ip to number and number to ip:

  • ip2long converts ip string to number
  • long2ip converts number to IPv4 string

Edit:

I've created library for calculations using math and bit operations

to use composer require rpodwika/network-calculator

https://github.com/rpodwika/network-calculator

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85