Forum Moderators: coopster

Message Too Old, No Replies

ip address validation regex fails

         

ethan

1:24 am on Jul 15, 2004 (gmt 0)

10+ Year Member



Hi there,

I have the following rexeg to validate an ip address. The thing I'm going crazy is that this regex does work perfectly when tested with The Regex Coach program, but when in php, it simply doesn't match.

Can someone tell me why?
thx.

code:


$ip = "192.168.0.19";

if (ereg("^\b((25[0-5]¦2[0-4]\d¦[01]\d\d¦\d?\d)\.){3}(25[0-5]¦2[0-4]\d¦[01]\d\d¦\d?\d)\b$", $ip)) {
echo 'ok';
} else {
echo 'no way...';
}

Timotheos

3:36 am on Jul 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ethan,

The Regex Coach is great, isn't it? The problem may lie in the fact that The Regex Coach uses Perl compatible regex [us2.php.net]. This is from their documentation:

Of course, this application should also be useful to programmers using Perl-compatible regex toolkits like PCRE (which is used by projects like Python, Apache, and PHP)

So you should be using preg_match. What you've got in ereg is POSIX extended regex [us3.php.net]. Betcha that'll make a difference.

Tim

ethan

8:50 am on Jul 15, 2004 (gmt 0)

10+ Year Member




Timotheos thanks for the information,

I changed to preg_match and now works perfectly, lots of thanks.

Here is the final regexp:


if (preg_match("{^\b((25[0-5]¦2[0-4]\d¦[01]\d\d¦\d?\d)\.){3}(25[0-5]¦2[0-4]\d¦[01]\d\d¦\d?\d)\b$}", $ip)) {
echo 'ok';
} else {
echo 'no way...';
}