php 判断邮箱地址的正则表达式详解
在php中我们经常会来利用正则表达式来验证用户输入的信息是不是邮箱地址了,下面我来给大家介绍判断邮箱地址的正则表达式详解
判断邮件的一个正则表达式,逐句解释下是什么意思,代码如下:
- ^(w+((-w+)|(.w+))*)+w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$
 
^ 匹配字符串头
(w+((-w+)|(.w+))*) 1:这里匹配laidfj456、sfi-lsoke、fe.23i这样的字符串
+ 匹配加号
w+((-w+)|(.w+))* 同1
@ 匹配@
[A-Za-z0-9]+2: 由大小写字母和数字?成的字符串,等价于w+
((.|-)[A-Za-z0-9]+)* 匹配0个或多个由"."或"-"开头的字符串,如.oeiu234mJ、-oiwuer4
. 匹配"."
[A-Za-z0-9]+ 同2
$ 匹配字符串的?尾
实例代码如下:
- <?php
 - /**
 - * 邮件的正则表达式 @author:lijianghai
 - */
 - function isEmail($input = null)
 - { //用户名:以数字、字母、下滑线组成;
 - $email = $input;
 - /*使用preg_ereg()出错:因为第二个参数需要是数组
 - * if(preg_grep("^[a-zA-Z][a-zA-Z0-9_]{3,19}@[0-9A-Za-z]{1,10}(.)(com|cn|net|com.cn)$", array($input)))
 - {
 - echo $email.'是符合条件的邮件地址';
 - }else
 - {
 - echo $email.'格式错误';
 - }
 - */
 - if(ereg("^[a-zA-Z][a-zA-Z0-9_]{3,9}@[0-9a-zA-Z]{1,10}(.)(com|cn|com.cn|net)$",$email))
 - {
 - echo $email."符合格式规范";
 - }
 - else
 - {
 - echo $email.'格式错误';
 - }
 - }
 - $email = "";
 - isEmail($email);
 - ?>