Jump to content

Regex for an IP address?


gamefreak13

Recommended Posts

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>
<?

}

?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.