Email regex PHP
The regular expressions below can be used to validate if a string is an email address and to extract email addresses from a string. This validation method however does not guarantee that the emails validated and extracted actually exist.
The basic validation
A simple PHP regex to validate string against email format and catch the most obvious syntax errors:
True
False
Enter a text in the input above to see the result
Example code in PHP:
The more complex email regex
This PHP regular expression will match 99% of valid email addresses and will not pass validation for email addresses that have, for instance:
- Dots in the beginning
- Multiple dots at the end
But at the same time it will allow part after @ to be IP address.
True
False
Enter a text in the input above to see the result
Example code in PHP:
RFC 5322 compliant regex
This PHP regular expression is compliant to RFC 5322 standard which allows for the most complete validation. Usually, you should not use it because it is an overkill. In most cases apps are not able to handle all emails that this regex allows.
True
False
Enter a text in the input above to see the result
Example code in PHP:
Extra information about validating email
As was stated previously, PHP regex email validation can not fully guarantee that email exists and the message can be delivered. The best way how to know for sure that email is valid is to actually send an email to that address because even paid email validation services do not provide a 100% guarantee for that.
It's better to use filter_var in most cases: