Guest User

Untitled

a guest
Dec 16th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /**
  2. * Verifies that an email is valid.
  3. *
  4. * Does not grok i18n domains. Not RFC compliant.
  5. *
  6. * @since 0.71
  7. *
  8. * @param string $email Email address to verify.
  9. * @param boolean $deprecated Deprecated.
  10. * @return string|bool Either false or the valid email address.
  11. */
  12. function is_email( $email, $deprecated = false ) {
  13. if ( ! empty( $deprecated ) )
  14. _deprecated_argument( __FUNCTION__, '3.0' );
  15.  
  16. // Test for the minimum length the email can be
  17. if ( strlen( $email ) < 3 ) {
  18. return apply_filters( 'is_email', false, $email, 'email_too_short' );
  19. }
  20.  
  21. // Test for an @ character after the first position
  22. if ( strpos( $email, '@', 1 ) === false ) {
  23. return apply_filters( 'is_email', false, $email, 'email_no_at' );
  24. }
  25.  
  26. // Split out the local and domain parts
  27. list( $local, $domain ) = explode( '@', $email, 2 );
  28.  
  29. // LOCAL PART
  30. // Test for invalid characters
  31. if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
  32. return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
  33. }
  34.  
  35. // DOMAIN PART
  36. // Test for sequences of periods
  37. if ( preg_match( '/\.{2,}/', $domain ) ) {
  38. return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
  39. }
  40.  
  41. // Test for leading and trailing periods and whitespace
  42. if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
  43. return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
  44. }
  45.  
  46. // Split the domain into subs
  47. $subs = explode( '.', $domain );
  48.  
  49. // Assume the domain will have at least two subs
  50. if ( 2 > count( $subs ) ) {
  51. return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
  52. }
  53.  
  54. // Loop through each sub
  55. foreach ( $subs as $sub ) {
  56. // Test for leading and trailing hyphens and whitespace
  57. if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
  58. return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
  59. }
  60.  
  61. // Test for invalid characters
  62. if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
  63. return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
  64. }
  65. }
  66.  
  67. // Congratulations your email made it!
  68. return apply_filters( 'is_email', $email, $email, null );
  69. }
Add Comment
Please, Sign In to add comment