The PHP email regular expression code is as follows:
/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/
Example
$mail = 'example@example.com'; //Email address
$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
preg_match($pattern, $mail, $matches);
var_dump($matches); //Output matching results
The above code produces the following output:
array(4) {
[0]=>
string(17) "example@example.com"
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(4) ".com"
}
Using the FILTER_VALIDATE_EMAIL filter
Example
$email = 'example@example.com'; //Email address
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailMsg = "Correct email format";
} else {
$emailMsg = "Invalid email format";
}
echo $emailMsg;