Checking Valid Emaill address with Regular Expression

This is an example from Wicked Cool PHP, an  very handy book for daily PHP coding.

I am reading this book now.  It has 12 chapters, total 76 tips.

function is_email($email){

   if (! preg_match('/^[A-Za-z0-9!#$%&\'*+-/=?^_`{|}~]+@[A-Za-z0-9]+(\.[A-Za-z0-9]+)[A-Za-z]$/', $email)){
        return false;
    } else {
        return true;
    }
}

The script above utilizes a regular expression to check whether the given email uses proper email characters, and @ sign in the middle, and at least one dot-something on the end.

  • //or also:
    return (bool)filter_var($email,FILTER_VALIDATE_EMAIL); // <3

  • //or also:
    return (bool)filter_var($email,FILTER_VALIDATE_EMAIL); // <3