Regex or filter_var

Hi All

I was wondering which of the following is more reliable method to test a valid email address:

  1. Regex
  2. Using PHP’s built-in function filter_var($email, FILTER_VALIDATE_EMAIL)

Does the latter one follow wc3 standard?

Thanks

Based on the first comment at http://php.net/manual/en/function.filter-var.php

Keep in mind that FILTER_VALIDATE_EMAIL will validate the email address according to standards.
However, giving the fact that organizations are free to restrict the forms of their own email addresses, using ONLY this filter can you a lot of bounces.

gmail, yahoo, hotmail, aol have special rules

I’m still trying to find official documentation that states that as well.

Still not what I wanted to find, but this bug report indicates it is based on the RFC3696/RFC5322 standards
https://bugs.php.net/bug.php?id=49576

I didnt get this line

using ONLY this filter can you a lot of bounces.

What has bounces to do with email validations?

Thanks

Yeah, that is a confusing statement, but here is what the person is trying to say:

The spec states you can have an email address starting with a number (so 0hotmail@hotmail.com will validate to true). However hotmail.com WILL NOT let you have said e-mail address, they do not permit an email address starting with a number.

So you can’t use filter_var to determine if an email address actually exists, you can only use it to validate its’ format (which is what it was designed for). If you want to verify the email address is valid (has an actual mailbox behind it), you would have to use a different process.

So, disregard the user’s statement, as it isn’t valid for the purpose that filter_var is designed to handle.

Always use a built in function for validation where one is available and only use a regex when it isn’t.

Anyway:

filter_var($email, FILTER_VALIDATE_EMAIL)

is a lot shorter than:

preg_match('/^[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|"[^\\\\\\x80-\\xff\
\\015"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015"]*)*")[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:\\.[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|"[^\\\\\\x80-\\xff\
\\015"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015"]*)*")[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)*@[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:\\.[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)*|(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|"[^\\\\\\x80-\\xff\
\\015"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015"]*)*")[^()<>@,;:".\\\\\\[\\]\\x80-\\xff\\000-\\010\\012-\\037]*(?:(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)|"[^\\\\\\x80-\\xff\
\\015"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015"]*)*")[^()<>@,;:".\\\\\\[\\]\\x80-\\xff\\000-\\010\\012-\\037]*)*<[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:@[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:\\.[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)*(?:,[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*@[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:\\.[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)*)*:[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)?(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|"[^\\\\\\x80-\\xff\
\\015"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015"]*)*")[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:\\.[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|"[^\\\\\\x80-\\xff\
\\015"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015"]*)*")[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)*@[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:\\.[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*(?:[^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\
\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\	]*(?:\\([^\\\\\\x80-\\xff\
\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\
\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\
\\015()]*)*\\))[^\\\\\\x80-\\xff\
\\015()]*)*\\)[\\040\	]*)*)*>)$/',$email)

which is an equivalent regex for validating email addresses.

I think the regex being used by filter_var($email, FILTER_VALIDATE_EMAIL) is

 "/^(?!(?:(?:\\\\x22?\\\\x5C[\\\\x00-\\\\x7E]\\\\x22?)|(?:\\\\x22?[^\\\\x5C\\\\x22]\\\\x22?)){255,})(?!(?:(?:\\\\x22?\\\\x5C[\\\\x00-\\\\x7E]\\\\x22?)|(?:\\\\x22?[^\\\\x5C\\\\x22]\\\\x22?)){65,}@)(?:(?:[\\\\x21\\\\x23-\\\\x27\\\\x2A\\\\x2B\\\\x2D\\\\x2F-\\\\x39\\\\x3D\\\\x3F\\\\x5E-\\\\x7E]+)|(?:\\\\x22(?:[\\\\x01-\\\\x08\\\\x0B\\\\x0C\\\\x0E-\\\\x1F\\\\x21\\\\x23-\\\\x5B\\\\x5D-\\\\x7F]|(?:\\\\x5C[\\\\x00-\\\\x7F]))*\\\\x22))(?:\\\\.(?:(?:[\\\\x21\\\\x23-\\\\x27\\\\x2A\\\\x2B\\\\x2D\\\\x2F-\\\\x39\\\\x3D\\\\x3F\\\\x5E-\\\\x7E]+)|(?:\\\\x22(?:[\\\\x01-\\\\x08\\\\x0B\\\\x0C\\\\x0E-\\\\x1F\\\\x21\\\\x23-\\\\x5B\\\\x5D-\\\\x7F]|(?:\\\\x5C[\\\\x00-\\\\x7F]))*\\\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\\\]))$/i";