phperegi

Eregi function is deprecated


Here is my code (from submit.php) that is throwing an error:

$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}

I know I need to use preg_match, but I don't know how to implement it. I've read the documentation, but I still don't get it. Thanks!


Solution

  • $email_exp = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
     if(preg_match($email_exp,$email_from) != 1) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    

    Actually, it'is very simple and very similar with eregi.