你如何规范化PHP中的电子邮件地址?

时间:2023-01-05 04:18:06

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

PHP中是否有可以规范化电子邮件地址的功能?

E.g., if case isn't significant, then FOO@example.com -> foo@example.com.

例如,如果情况不重要,则为FOO@example.com - > foo@example.com。

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

我不知道电子邮件地址应该被视为“相同”的规则,所以我不想自己实现。

7 个解决方案

#1


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

*综合了各种RFC如何形成电子邮件地址的说法。

Despite what others have said, email can be case sensitive

尽管其他人已经说过,但电子邮件可以区分大小写

The local-part is case sensitive, so "jsmith@example.com" and "JSmith@example.com" 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.

本地部分区分大小写,因此“jsmith@example.com”和“JSmith@example.com”可能会传递给不同的人。 RFC 5321不鼓励这种做法。但是,只有域的权威邮件服务器才能做出该决定。唯一的例外是“postmaster”的本地部分值,它不区分大小写,应该转发给服务器的管理员。

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).

因此,就您的具体问题(案例规范化)而言,您可以小写服务器部分(在@的右侧),但是您最好看到合适(由@,strToLower拆分服务器组件,重新组合)。

#2


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:

如果需要,可以使用strtolower(),它可以正确覆盖大部分电子邮件。但是,如果您想要正确执行此操作,还有一些其他信息:

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 soulmerge@example.com, soulmerge+friends@example.com and SOULMERGE@example.com 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.

对于一个邮件服务器soulmerge@example.com,soulmerge +friends@example.com和SOULMERGE@example.com可能是同一个邮箱,而在另一个邮件服务器中它可能指向两个或三个不同的邮箱,但事实是:你无法知道。您在整个地址上进行的任何翻译都可能导致地址无效。

#3


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

使用strtolower()将服务器部分设置为小写。 (由于之前的回答而更新)

$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.

此外,如果您想要标准化格式,您可能还需要查看filter_var(),它可以对电子邮件地址进行整理/验证,以及其他几种格式。

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

首先,FILTER_SANITIZE_EMAIL将确保其中没有非法字符。

$email_sanatized = filter_var('bob@example.com', FILTER_SANITIZE_EMAIL);

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

然后,FILTER_VALIDATE_EMAIL将确保它采用有效的电子邮件格式

$email = filter_var($email_sanatized, FILTER_VALIDATE_EMAIL);

#4


this is just a complement of other answers.

这只是其他答案的补充。

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

在gmail的情况下,我会删除左侧的点。

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 yourusername@gmail.com, no one can ever register your.username@gmail.com, or Your.user.name@gmail.com. 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.

Gmail只允许对任何给定用户名进行一次注册。注册用户名后,其他人无法注册相同的用户名,无论其是否包含额外的句点或大写字母;那些用户名属于你。如果您创建了yourusername@gmail.com,则无法注册your.username@gmail.com或Your.user.name@gmail.com。由于Gmail无法将点识别为用户名中的字符,因此您可以在不更改实际目标地址的情况下在Gmail地址中添加或删除这些点;他们都会进入你的收件箱,只有你自己。

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

所以你可以确定你总是有相同的Gmail电子邮件。

#5


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

修剪所有空格,然后与strtolower进行比较。那应该是完全没问题的。

#6


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

如果你正在寻找小写:strtolower()。

#7


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 );
}

#1


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

*综合了各种RFC如何形成电子邮件地址的说法。

Despite what others have said, email can be case sensitive

尽管其他人已经说过,但电子邮件可以区分大小写

The local-part is case sensitive, so "jsmith@example.com" and "JSmith@example.com" 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.

本地部分区分大小写,因此“jsmith@example.com”和“JSmith@example.com”可能会传递给不同的人。 RFC 5321不鼓励这种做法。但是,只有域的权威邮件服务器才能做出该决定。唯一的例外是“postmaster”的本地部分值,它不区分大小写,应该转发给服务器的管理员。

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).

因此,就您的具体问题(案例规范化)而言,您可以小写服务器部分(在@的右侧),但是您最好看到合适(由@,strToLower拆分服务器组件,重新组合)。

#2


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:

如果需要,可以使用strtolower(),它可以正确覆盖大部分电子邮件。但是,如果您想要正确执行此操作,还有一些其他信息:

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 soulmerge@example.com, soulmerge+friends@example.com and SOULMERGE@example.com 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.

对于一个邮件服务器soulmerge@example.com,soulmerge +friends@example.com和SOULMERGE@example.com可能是同一个邮箱,而在另一个邮件服务器中它可能指向两个或三个不同的邮箱,但事实是:你无法知道。您在整个地址上进行的任何翻译都可能导致地址无效。

#3


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

使用strtolower()将服务器部分设置为小写。 (由于之前的回答而更新)

$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.

此外,如果您想要标准化格式,您可能还需要查看filter_var(),它可以对电子邮件地址进行整理/验证,以及其他几种格式。

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

首先,FILTER_SANITIZE_EMAIL将确保其中没有非法字符。

$email_sanatized = filter_var('bob@example.com', FILTER_SANITIZE_EMAIL);

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

然后,FILTER_VALIDATE_EMAIL将确保它采用有效的电子邮件格式

$email = filter_var($email_sanatized, FILTER_VALIDATE_EMAIL);

#4


this is just a complement of other answers.

这只是其他答案的补充。

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

在gmail的情况下,我会删除左侧的点。

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 yourusername@gmail.com, no one can ever register your.username@gmail.com, or Your.user.name@gmail.com. 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.

Gmail只允许对任何给定用户名进行一次注册。注册用户名后,其他人无法注册相同的用户名,无论其是否包含额外的句点或大写字母;那些用户名属于你。如果您创建了yourusername@gmail.com,则无法注册your.username@gmail.com或Your.user.name@gmail.com。由于Gmail无法将点识别为用户名中的字符,因此您可以在不更改实际目标地址的情况下在Gmail地址中添加或删除这些点;他们都会进入你的收件箱,只有你自己。

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

所以你可以确定你总是有相同的Gmail电子邮件。

#5


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

修剪所有空格,然后与strtolower进行比较。那应该是完全没问题的。

#6


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

如果你正在寻找小写:strtolower()。

#7


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 );
}