This article demonstrates how to validate an email address in PHP.

1. Using filter_var() function

The filter_var() function filters a variable using a specified filter and returns the filtered data. You can call filter_var() with the FILTER_VALIDATE_EMAIL filter to validate the email address against the addr-spec syntax in RFC-822.

Download  Run Code

 
The PHP documentation states that this function will not validate “not latin” domains. Also, it can conflict with RFC-5321 addresses. For example,

2. Using preg_match() function

You can also use a regex to validate email addresses using the preg_match() function. The following regex is taken from the FILTER_VALIDATE_EMAIL source. You can use and redistribute this regex, but keep the below copyright notice.

Download  Run Code

That’s all about validating an email address in PHP.