/^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,})$/
实例
<?php
$mail = 'runoob@runoob.com'; //邮箱地址
$pattern = \"大众/^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,})$/\公众;
preg_match($pattern, $mail, $matches);
var_dump($matches); //输出匹配结果
?>
以上代码运行输出结果为:
array(4) {
[0]=>
string(17) \"大众runoob@runoob.com\"大众
[1]=>
string(0) \"大众\公众
[2]=>
string(0) \"大众\"大众
[3]=>
string(4) \"大众.com\"大众
}
利用 FILTER_VALIDATE_EMAIL 过滤器
实例
<?php
$email = 'runoob@runoob.com'; //邮箱地址
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailMsg = \"大众造孽邮箱格式\"大众;
} else {
$emailMsg = \"大众精确邮箱格式\公众;
}
echo $emailMsg;