2

Possible Duplicate:
Is there a PHP library for email address validation?

How can I verify an email address in PHP?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fernando123
  • 3,559
  • 2
  • 15
  • 6
  • http://stackoverflow.com/questions/5737290/using-a-regular-expression-to-validate-email-addresses – Thilo May 01 '11 at 14:59

3 Answers3

5

If you want to check if an email address exists, check out checkdnsrr:

if (checkdnsrr('you@user.com', 'MX')) {
    echo ':)';
}
else {
   echo ':(';
}

If you want to check if a string is an email address, there are literally millions of regulars expressions out there. There's a native function in php, filter_input that does it, thought I've personally never used it.

Zirak
  • 38,920
  • 13
  • 81
  • 92
  • Tried with my personal email. Didn't work. – Explosion Pills May 01 '11 at 14:55
  • Requires PHP 5.3+ or a non-Windows platform to work. – Arc May 01 '11 at 15:28
  • Didn't work for 5 of my email addresses (3 of them are Google Apps accounts, one of it is Hotmail and the last one is a regular GMail one). I am using PHP 5.3.3-7 on a Debian system. – Mathieu Rodic Jul 09 '11 at 19:00
  • 1
    That's because the 'checkdnsrr' function does not accept e-mail addresses! `if(checkdnsrr(array_pop(explode("@",$email)),'MX'))` `echo "valid e-mail address";` `else` `echo "invalid e-mail address";` – Markus Mar 11 '12 at 14:05
5

filter_var($input, FILTER_VALIDATE_EMAIL)

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Might be PHP-version-dependent, but may not fully support international domain names with Unicode characters (IDNs). – Arc May 01 '11 at 15:29
5
function isemail($email) {
    return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
iSnowpine
  • 51
  • 3