bloodgoat Posted May 12, 2009 Share Posted May 12, 2009 Have an input field to add IP addresses to ban list. I just need to know how to filter it to make sure that the syntax was correct (eg/ xxx.xx.xx.xxx). Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/ Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Well with your example - preg_match('#\d{3}\.(\d{2}\.){2}\d{3}#', $ip); Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-832824 Share on other sites More sharing options...
MadTechie Posted May 13, 2009 Share Posted May 13, 2009 A little better would be a valid IPv4 address Use this regex to match IP numbers with accurracy. preg_match('/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/', $IP); Matches 0.0.0.0 through 255.255.255.255 Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-832886 Share on other sites More sharing options...
.josh Posted May 13, 2009 Share Posted May 13, 2009 Hate to burst your bubble MadTechie, but your regex will return true if the first and/or last node is greater than 255 and also it will validate if there are 4+ nodes. I nonetheless did a benchmark, rigging it to always be a valid IP address and never be a valid IP address. This function for all intents and purposes validates at worst, same speed as your pattern, at best, twice as fast (this function will return a false up to twice as fast as your regex). Suppose it's not really fair comparing it to a broken regex, but I have a sneaking suspicion any regex you throw at it will give about the same results, with all the alternation that must inevitably be used... function isValidIP($ip) { $ip = explode('.',$ip); if (count($ip) != 4) return false; foreach ($ip as $node) { if (($node > 255) || ($node < 0) || !is_numeric($node)) return false; } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-832923 Share on other sites More sharing options...
MadTechie Posted May 13, 2009 Share Posted May 13, 2009 Hate to burst your bubble MadTechie, but your regex will return true if the first and/or last node is greater than 255 and also it will validate if there are 4+ nodes. Not true it captures all 4, of course you could use ^ + $ the RegEx works fine.. can you post a IP it fails with !! ie if (preg_match('/\A(?:^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)\Z/im', $ip)) { echo "$ip passed"; } else { echo "$ip failed"; } 253.26.54.4 = passed 255.235.12.2 = passed 256.12.23.45 = failed Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-833535 Share on other sites More sharing options...
Axeia Posted May 13, 2009 Share Posted May 13, 2009 Heh, this one is actually in the example section regular-expressions.info http://www.regular-expressions.info/examples.html Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-833560 Share on other sites More sharing options...
nrg_alpha Posted May 13, 2009 Share Posted May 13, 2009 I think the php devteam should come up with a new function: is_ip() Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-833586 Share on other sites More sharing options...
Philip Posted May 14, 2009 Share Posted May 14, 2009 Well - ip2long kinda does that. Returns false on invalid IP - but I agree, it would be nice to have a built in function is_ip Quote Link to comment https://forums.phpfreaks.com/topic/157889-how-to-identify-if-the-input-string-is-valid-ip-address-format/#findComment-834266 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.