Code for validating email in php

Email addresses: easy to create, difficult to type. Note that FILTER_VALIDATE_EMAIL will validate email addresses that contain domains and top-level domains that do not exist. Fortunately, PHP (5 and later) comes with a handy set of functions and filters that make testing for email address validity a snap.

FILTER_VALIDATE_EMAIL will erroneously balk at email addresses with long domain names (64 characters or more), and at email addresses with escaped characters (such as "me\"").

Now that you have your easy answer feel free to read on about email address validation if you care to learn or otherwise just use the fast answer and move on. Trying to validate an email address using a regex is an "impossible" task.

Check out this list for tests (both failed and succeeded) of the regex used by PHP's is the best option.

If you want to know which regex pattern PHP (currently) uses to validate email addresses see the PHP source.

Getting email addresses you collect — for a newsletter, say, or for password retrieval — to at least conform to standards (if not ensure ) is crucial, of course, and extremely tricky.