is_email

Verifies that an email is valid.

Description

(string|bool) is_email( (string) $email, (bool) $deprecated = false ); 

Does not grok i18n domains. Not RFC compliant.

Returns (string|bool)

Either false or the valid email address.

Parameters (2)

0. $email (string)
Email address to verify.
1. $deprecated — Optional. (bool) => false
The deprecated.

Usage

  1. if ( !function_exists( 'is_email' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // Email address to verify. 
  5. $email = ''; 
  6.  
  7. // The deprecated. 
  8. $deprecated = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = is_email($email, $deprecated); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function is_email( $email, $deprecated = false ) { 
  2. if ( ! empty( $deprecated ) ) 
  3. _deprecated_argument( __FUNCTION__, '3.0.0' ); 
  4.  
  5. // Test for the minimum length the email can be 
  6. if ( strlen( $email ) < 3 ) { 
  7. /** 
  8. * Filters whether an email address is valid. 
  9. * This filter is evaluated under several different contexts, such as 'email_too_short',  
  10. * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',  
  11. * 'domain_no_periods', 'sub_hyphen_limits', 'sub_invalid_chars', or no specific context. 
  12. * @since 2.8.0 
  13. * @param bool $is_email Whether the email address has passed the is_email() checks. Default false. 
  14. * @param string $email The email address being checked. 
  15. * @param string $context Context under which the email was tested. 
  16. */ 
  17. return apply_filters( 'is_email', false, $email, 'email_too_short' ); 
  18.  
  19. // Test for an @ character after the first position 
  20. if ( strpos( $email, '@', 1 ) === false ) { 
  21. /** This filter is documented in wp-includes/formatting.php */ 
  22. return apply_filters( 'is_email', false, $email, 'email_no_at' ); 
  23.  
  24. // Split out the local and domain parts 
  25. list( $local, $domain ) = explode( '@', $email, 2 ); 
  26.  
  27. // LOCAL PART 
  28. // Test for invalid characters 
  29. if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) { 
  30. /** This filter is documented in wp-includes/formatting.php */ 
  31. return apply_filters( 'is_email', false, $email, 'local_invalid_chars' ); 
  32.  
  33. // DOMAIN PART 
  34. // Test for sequences of periods 
  35. if ( preg_match( '/\.{2, }/', $domain ) ) { 
  36. /** This filter is documented in wp-includes/formatting.php */ 
  37. return apply_filters( 'is_email', false, $email, 'domain_period_sequence' ); 
  38.  
  39. // Test for leading and trailing periods and whitespace 
  40. if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) { 
  41. /** This filter is documented in wp-includes/formatting.php */ 
  42. return apply_filters( 'is_email', false, $email, 'domain_period_limits' ); 
  43.  
  44. // Split the domain into subs 
  45. $subs = explode( '.', $domain ); 
  46.  
  47. // Assume the domain will have at least two subs 
  48. if ( 2 > count( $subs ) ) { 
  49. /** This filter is documented in wp-includes/formatting.php */ 
  50. return apply_filters( 'is_email', false, $email, 'domain_no_periods' ); 
  51.  
  52. // Loop through each sub 
  53. foreach ( $subs as $sub ) { 
  54. // Test for leading and trailing hyphens and whitespace 
  55. if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) { 
  56. /** This filter is documented in wp-includes/formatting.php */ 
  57. return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' ); 
  58.  
  59. // Test for invalid characters 
  60. if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) { 
  61. /** This filter is documented in wp-includes/formatting.php */ 
  62. return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' ); 
  63.  
  64. // Congratulations your email made it! 
  65. /** This filter is documented in wp-includes/formatting.php */ 
  66. return apply_filters( 'is_email', $email, $email, null );