How to check for a duplicate email address in PHP, considering Gmail (user.name+label@gmail.com)

前端 未结 7 1377
孤城傲影
孤城傲影 2021-01-01 04:48

How can I check for duplicate email addresses in PHP, with the possibility of Gmail\'s automated labeler and punctuation in mind?

For example, I wan

7条回答
  •  -上瘾入骨i
    2021-01-01 05:12

    This answer is an improvement on @powtac's answer. I needed this function to defeat multiple signups from same person using gmail.

    if ( ! function_exists('normalize_email'))
    {
        /**
         * to normalize emails to a base format, especially for gmail
         * @param $email
         * @return string
         */
        function normalize_email($email) {
            // ensure email is lowercase because of pending in_array check, and more...
            $email = strtolower($email);
            $parts    = explode('@', $email);
    
            // normalize gmail addresses
            if (in_array($parts[1], ['gmail.com', 'googlemail.com'])) {
                // check if there is a "+" and return the string before then remove "."
                $before_plus    = strstr($parts[0], '+', TRUE);
                $before_at      = str_replace('.', '', $before_plus ? $before_plus : $parts[0]);
    
                // ensure only @gmail.com addresses are used
                $email    = $before_at.'@gmail.com';
            }
    
            return $email;
        }
    }
    

提交回复
热议问题