gamefreak13 Posted May 23, 2008 Share Posted May 23, 2008 I will have some user input, and instead of tying to sanitizing it, I want to use a regex. I want it to say "###.###.###.###" is ok.. or "any numbers and periods". I found this little script that was originally made for an email regex (which worked great). So I edited it to let me plug in a regex and test it. All I need is the regex for an IP address. The one I made works fine for "#.#.#.#" but I need it to accept up to 3 numbers per "block". <? if($_REQUEST['action'] == 'validate') { // The commented one does not work at all. //if(eregi('^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$', $_REQUEST['ipaddress'])) { if(eregi('^[0-9]\.[0-9]\.[0-9]\.[0-9]$', $_REQUEST['ipaddress'])) { echo 'Valid'; } else { echo 'Invalid'; } } else { ?> <form action="<?=$_SERVER['PHP_SELF']; ?>" method="POST"> <input type="text" name="ipaddress"> <input type="hidden" name="action" value="validate"> <input type="submit" value="Submit"> </form> <? } ?> Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 23, 2008 Author Share Posted May 23, 2008 Ok this one seems to work. I'm not experienced with regex, so maybe someone who is can double check it for me? Thanks! if(eregi('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$', $_REQUEST['ipaddress'])) { 1.1.1.1 works (good) 11.11.11.11 works (good) 111.111.111.111 works (good) a.a.a.a does not work (good) aa.aa.aa.aa does not work (good) aaa.aaa.aaa.aaa does not work (good) aaaaaaaaaaaa does not work (good) #$%^$#%&^^%*&^* does not work (good) < b>hello< /b> does not work (good) Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 23, 2008 Author Share Posted May 23, 2008 Just wondering if there is any problem with my regex? To a novice regexer (haha) it appears to be fine.. but I'd like to make sure it's solid. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 23, 2008 Share Posted May 23, 2008 It looks fine, but there's no need to use eregi with digits and periods; use ereg. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 23, 2008 Share Posted May 23, 2008 Looks ok, but i'd tidy it up to checking for 1-3 digits followed by a period 3 times, followed by 1-3 digits: <?php $ip = '10.0.0.1'; if(preg_match('|^([0-9]{1,3}\.){3}[0-9]$|',$ip)){ echo 'valid'; }else{ echo 'not valid'; } ?> Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 23, 2008 Author Share Posted May 23, 2008 I just googled the difference between ereg and eregi, and your right.. so I'll switch it to ereg. However, what is the difference between ereg and preg_match? Everywhere I read says preg_match is faster but is more complicated. I also think I read that ereg and ereg are being phased out in the near future.. although I might be wrong.. it may have been a different funciton I read. As for the regex, is the only difference between mine and yours that its shorter code? What do the | characters mean? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 23, 2008 Share Posted May 23, 2008 No, you're correct -- ereg is being dropped altogether i believe in PHP 6. I wouldn't say the syntax for preg_match is any harder, you just have more options. And no, there's no real difference between the patterns. Mine just cut out the repetition. The | character is being used as a delimiter. With PCRE, you need to give a start and end character between which the pattern is contained. It's up to you what you choose, but the character cannot appear inside the pattern unless it is escaped. You can then add modifiers after the closing delimiter, such as i which makes the pattern case insensitive. Quote Link to comment 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.