Advertisement
manchumahara

Email validation in Wordpress, Removed wp specific filter

May 27th, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 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 ) {
  13.    
  14.  
  15.     // Test for the minimum length the email can be
  16.     if ( strlen( $email ) < 3 ) {
  17.         return false;
  18.     }
  19.  
  20.     // Test for an @ character after the first position
  21.     if ( strpos( $email, '@', 1 ) === false ) {
  22.         return false;
  23.     }
  24.  
  25.     // Split out the local and domain parts
  26.     list( $local, $domain ) = explode( '@', $email, 2 );
  27.  
  28.     // LOCAL PART
  29.     // Test for invalid characters
  30.     if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
  31.         return false;
  32.     }
  33.  
  34.     // DOMAIN PART
  35.     // Test for sequences of periods
  36.     if ( preg_match( '/\.{2,}/', $domain ) ) {
  37.         return false;
  38.     }
  39.  
  40.     // Test for leading and trailing periods and whitespace
  41.     if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
  42.         return false;
  43.     }
  44.  
  45.     // Split the domain into subs
  46.     $subs = explode( '.', $domain );
  47.  
  48.     // Assume the domain will have at least two subs
  49.     if ( 2 > count( $subs ) ) {
  50.         return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
  51.     }
  52.  
  53.     // Loop through each sub
  54.     foreach ( $subs as $sub ) {
  55.         // Test for leading and trailing hyphens and whitespace
  56.         if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
  57.             return false;
  58.         }
  59.  
  60.         // Test for invalid characters
  61.         if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
  62.             return false;
  63.         }
  64.     }
  65.  
  66.     // Congratulations your email made it!
  67.     return $email;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement