allyourcode
allyourcode

Reputation: 22603

How do you normalize an email address in PHP?

Is there a function in PHP that can normalize an email address?

E.g., if case isn't significant, then [email protected] -> [email protected].

I don't know the rules for when email addresses should be considered "the same", so I don't want to implement this myself.

Upvotes: 3

Views: 3711

Answers (7)

Tyler Carter
Tyler Carter

Reputation: 61567

Use strtolower() to make the server portion lowercase. (Updated due to previous answer)

$parts = explode("@", $email);
$host = strtolower($parts[1]);
$email = $parts[0]."@".$host;

Also, if you want to standardize the format aswell, you probably want to look into filter_var(), which can sanatize/validate email addresses, along with several other formats.

First, the FILTER_SANITIZE_EMAIL will make sure that there are no illegal characters in it.

$email_sanatized = filter_var('[email protected]', FILTER_SANITIZE_EMAIL);

Then, FILTER_VALIDATE_EMAIL will make sure it is in a valid email format

$email = filter_var($email_sanatized, FILTER_VALIDATE_EMAIL);

Upvotes: 4

Gabriel Sosa
Gabriel Sosa

Reputation: 7956

this is just a complement of other answers.

in the case of gmail, I would remove the dots on the left side.

Gmail allows only one registration for any given username. Once you sign up for a username, nobody else can sign up for the same username, regardless of whether it contains extra periods or capital letters; those usernames belong to you. If you created [email protected], no one can ever register [email protected], or [email protected]. Because Gmail doesn't recognize dots as characters within usernames, you can add or remove the dots from a Gmail address without changing the actual destination address; they'll all go to your inbox, and only yours.

so you can sure you always have the same gmail email.

Upvotes: 2

soulmerge
soulmerge

Reputation: 75704

If you want, you can use strtolower(), which could cover most of your emails correctly. But here is some additional info, if you want to do it correctly:

An email address consists of two parts: a local-part (anything before @), and a domain (anything after @). The local-part is meant to be interpreted by the mail server of the domain given in the domain part, so you actually cannot make any assumptions on that (case matters, for example!).

Many mail servers provide the option of adding arbitrary comments to your user name with a plus sign, like the following:

soulmerge+this_mail_is_delivered_to_the_user_soulmerge@example.com

For one mail server [email protected], [email protected] and [email protected] might be the same mail box, whereas in another it might point to two or three distinct mailboxes, but fact is: you cannot know. Any translation you make on the whole address might lead to an invalid address.

Upvotes: 4

Kane Wallmann
Kane Wallmann

Reputation: 2322

EDIT: Based on another answer that states only the domain is case insensitive I've updated the function to only lowercase the domain not the user.

function NormalizeEmail( $email )
{
    list( $user, $domain ) = explode( '@', trim( $email ) );
    return $user . '@' . strtolower( $domain );
}

Upvotes: -1

Alana Storm
Alana Storm

Reputation: 166066

Wikipedia has a roundup of what the various RFCs say about how an email address should be formed.

Despite what others have said, email can be case sensitive

The local-part is case sensitive, so "[email protected]" and "[email protected]" may be delivered to different people. This practice is discouraged by RFC 5321. However, only the authoritative mail servers for a domain may make that decision. The only exception is for a local-part value of "postmaster" which is case insensitive, and should be forwarded to the server's administrator.

The local part is referring to the part of the address to the left of the @ sign.

So, as far as your specific concern (case normalization), you could lowercase the server portion (to the right of the @) however you best see fit (split by the @, strToLower the server component, recombine).

Upvotes: 10

Matt Grande
Matt Grande

Reputation: 12157

Trim out all whitespace, then compare with strtolower. That should be perfectly fine.

Upvotes: 1

deceze
deceze

Reputation: 522081

If lowercasing is all you're looking for: strtolower().

Upvotes: -1

Related Questions