solarisuser Posted July 17, 2007 Share Posted July 17, 2007 Hi All, regex newb trying to figure this out.. I use /^(\d{1,3}\.){3}\d{1,3}$/ to validate IP addresses in a text field. I would like to add support for commas and spaces, so I can allow something like "192.168.0.1, 192.168.0.2" Thanks Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/ Share on other sites More sharing options...
effigy Posted July 17, 2007 Share Posted July 17, 2007 This will allow a comma with spaces, or just spaces: <pre> <?php $ips = array( '192.168.0.1, 192.168.0.2', '192.168.0.1 192.168.0.2', '192.168.0.1 192.168.0.2,', '192.168.0.1 192.168.0.2 ' ); foreach ($ips as $ip) { echo $ip, '<br>', preg_match('/^(??:\d{1,3}\.){3}\d{1,3}(?:\z|,?\s+))+\z/', $ip) ? 'Matches' : 'Does not match', '<br>'; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/#findComment-300661 Share on other sites More sharing options...
solarisuser Posted July 17, 2007 Author Share Posted July 17, 2007 It doesn't work =( I used http://www.codehouse.com/webmaster_tools/regex/ to verify... 1.1.1.1, 1.1.1.2 or 1.1.1.1,1.1.1.2 Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/#findComment-300666 Share on other sites More sharing options...
effigy Posted July 18, 2007 Share Posted July 18, 2007 It probably does not support PREG. If you plug those two values into the $ips array you'll see that the first works and the second does not (because the pattern is requiring at least one space since the comma is optional). Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/#findComment-300830 Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 try ^(??:\d{1,3}\.){3}\d{1,3}(?:\z|,?\s+))+ or use effigy code (better solution) Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/#findComment-301089 Share on other sites More sharing options...
solarisuser Posted July 19, 2007 Author Share Posted July 19, 2007 I'd like to modify the request. Using this regex: \b(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b I'd like to modify it to allow commas, followed by an additional IP, up to an unlimited amount of times, without having a comma at the end. For example: 192.168.1.1,192.168.1.2,192.168.1.3 , or just 192.168.1.1 Thanks - its been a tough one for me to try and figure out! Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/#findComment-302077 Share on other sites More sharing options...
MadTechie Posted July 19, 2007 Share Posted July 19, 2007 Try this.. seams to work fine <?php $IPs = "192.168.1.1,192.168.1.2,192.168.1.3"; //$IPs = "192.168.1.1"; preg_match_all('/\b(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]?)\b/', $IPs, $result, PREG_PATTERN_ORDER); $result = $result[0]; //print_r($result); foreach($result as $IP) { echo "$IP<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/60441-modify-existing-regex-to-support-commas-and-spaces/#findComment-302266 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.