/^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,})$/

实例

<?php

php正则emailPHP 邮箱正则表达式 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;