Match IPv4 address given IP range/mask?

后端 未结 6 704
一生所求
一生所求 2020-12-28 12:06

Either with PHP or a RegExp (or both), how do I match a range of IP addresses?

Sample Incoming IPs

10.210.12.12
10.253.12.12
10.210.12.254
10.210.1         


        
6条回答
  •  情深已故
    2020-12-28 12:11

    Regex really doesn't sound like the right tool to deal with subnet masks (at least not in decimal). It can be done, but it will be ugly.

    I strongly suggest parsing the string into 4 integers, combining to a 32-bit int, and then using standard bitwise operations (basically a bitwise-AND, and then a comparison).

提交回复
热议问题