Php get subnet from ip
Here is one fast 64bits function to do it, please comment the return line you don't need. Accepting any valid Ipv4 with or without valid CIDR Routing Prefix for example 63.161.156.0/24 or 63.161.156.0
To fast check if a given ipv4 is matching a given CIDR you can do it inline like in this example
To get the full range as an array for a given IP (with or without CIDR Routing Prefix) you can use the following code but be carefull because for example 25.25.25.25/16 return an array with 65536 elements and you can easily run out of memory using a smaller Routing Prefix
To fast check if a given ipv4 is matching a given array of IP (with or without CIDR Routing Prefix)
To run fast the function don't check input but formally it should be a string matching the following regex
If you want to verify the input before using the function
Then the formal answer to your question is the following
|