当前位置:首页 > PHP教程 > 正则表达式 > 列表

php中email邮件地址验证大全集合

发布:smiling 来源: PHP粉丝网  添加日期:2014-01-07 16:00:28 浏览: 评论:0 

在php中地址验证写法各种各样的,下面我来总结几种常用的email地址验证实例,最简单的是直接使用正则表达式preg_match(\"/^([a-z0-9\\+_\\-]+)(\\.[a-z0-9\\+_\\-]+)*@([a-z0-9\\-]+\\.)+[a-z]{2,6}$/ix来验证了。

CodeIgniter框架邮件地址验证,代码如下:

  1. /**  
  2.      * Valid Email  
  3.      *  
  4.      * @access  public  
  5.      * @param   string  
  6.      * @return  bool  
  7.      */ 
  8.     function valid_email($str)  
  9.     {  
  10.         return ( ! preg_match("/^([a-z0-9+_-]+)(.[a-z0-9+_-]+)*@([a-z0-9-]+.)+[a-z]{2,6}$/ix"$str)) ? FALSE : TRUE;  
  11.     } 
  12. PHPCMS邮件正则验证 
  13.  代码如下 复制代码 
  14. /**  
  15.  * 判断email格式是否正确  
  16.  * @param $email  
  17.  */ 
  18. function is_email($email) {  
  19.     return strlen($email) > 6 && preg_match("/^[w-.]+@[w-.]+(.w+)+$/"$email);  

WordPress邮件地址验证函数,代码如下:

  1. function is_email( $email$deprecated = false ) {  
  2.     if ( ! emptyempty$deprecated ) )  
  3.         _deprecated_argument( __FUNCTION__'3.0' );  
  4.    
  5.     // Test for the minimum length the email can be  
  6.     if ( strlen$email ) < 3 ) {  
  7.         return apply_filters( 'is_email', false, $email'email_too_short' );  
  8.     }  
  9.    
  10.     // Test for an @ character after the first position  
  11.     if ( strpos$email'@', 1 ) === false ) {  
  12.         return apply_filters( 'is_email', false, $email'email_no_at' );  
  13.     }  
  14.    
  15.     // Split out the local and domain parts  
  16.     list( $local$domain ) = explode'@'$email, 2 );  
  17.    
  18.     // LOCAL PART  
  19.     // Test for invalid characters  
  20.     if ( !preg_match( '/^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~.-]+$/', $local ) ) {  
  21.         return apply_filters( 'is_email', false, $email'local_invalid_chars' );  
  22.     }  
  23.    
  24.     // DOMAIN PART  
  25.     // Test for sequences of periods  
  26.     if ( preg_match( '/.{2,}/'$domain ) ) {  
  27.         return apply_filters( 'is_email', false, $email'domain_period_sequence' );  
  28.     }  
  29.    
  30.     // Test for leading and trailing periods and whitespace  
  31.     if ( trim( $domain" tnrx0B." ) !== $domain ) {  
  32.         return apply_filters( 'is_email', false, $email'domain_period_limits' );  
  33.     }  
  34.    
  35.     // Split the domain into subs  
  36.     $subs = explode'.'$domain );  
  37.    
  38.     // Assume the domain will have at least two subs  
  39.     if ( 2 > count$subs ) ) {  
  40.         return apply_filters( 'is_email', false, $email'domain_no_periods' );  
  41.     }  
  42.    
  43.     // Loop through each sub  
  44.     foreach ( $subs as $sub ) {  
  45.         // Test for leading and trailing hyphens and whitespace  
  46.         if ( trim( $sub" tnrx0B-" ) !== $sub ) {  
  47.             return apply_filters( 'is_email', false, $email'sub_hyphen_limits' );  
  48.         }  
  49.    
  50.         // Test for invalid characters  
  51.         if ( !preg_match('/^[a-z0-9-]+$/i'$sub ) ) {  
  52.             return apply_filters( 'is_email', false, $email'sub_invalid_chars' );  
  53.         }  
  54.     }  
  55.    
  56.     // Congratulations your email made it!  
  57.     return apply_filters( 'is_email'$email$email, null );  

下面分享一个自己写的实例,代码如下:

  1. $email = "tanklo_--vehy@yahoo.com.cn"
  2.     function check_email($email) { 
  3.        $pattern_test = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i"
  4.        return  preg_match($pattern_test,$email); 
  5.     } 
  6. echo check_email($email);

Tags: email 邮件地址 验证

分享到: