Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
I need help, below is a small VERY basic regex to somewhat validate an email, I do realize it does not work the greatest but for my needs it is ok for now.
It currently uses PHP's eregi function which php.net says is now a depreciated function and I should use preg_match instead, simply replacing erei with preg_match does not work, can someone show me how to make it work?
function validate_email($email) {
if (!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $email)) {
echo 'bad email';
} else {
echo 'good email';
}
}
function validate_email($email) {
if (!preg_match("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", $email)) {
echo 'bad email';
} else {
echo 'good email';
}
}
Perl-style regex patterns always need to be delimited. The very first character in the string is considered the delimiter, so something like this:
function validate_email($email) {
if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $email)) {
echo 'bad email';
} else {
echo 'good email';
}
}
The reason your initial attempt didn't work is because it was trying to use ^
as the delimiter character but (obviously) found no matching ^
for the end of the regex.
You will need to change three things
i
flag.Otherwise, the rest looks PCRE compatible (yes, that's kind of redundant =P)
"/^[a-z0-9][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With