Forum Moderators: coopster
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...';
}
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