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

在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);
  20. }

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

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

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

  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);