PHP + Preg_replace :删除电子邮件中“+”后面的部分


PHP + Preg_replace : Remove the part after '+' in email

为了推广网站,我允许订阅者通过电子邮件邀请朋友,并奖励他们。

但是,他们中的一些人使用了这样的电子邮件列表:

user@example.com, user+1@example.com, user+2@example.com

所有这些电子邮件都指向user@example.com因此它们都是有效的。

删除"+"号后面的部分的preg_replace模式是什么,以便我可以在那之后爆炸并使用array_unique删除重复的电子邮件?

谢谢。

很容易

$email = preg_replace('/'+[^@]*/i' , '' , $email);

我终于明白了。只需使用以下命令删除"+"和"@"之间的部分:

preg_replace('/'+(.)*@/', '@', $emails);

希望它会有所帮助。

PS:对不起,巴特,你以为我把我的工作交给你:如果我没有给出任何代码,那是因为它们都不起作用。

试试看

preg_replace('/(.+)'+.+(@.+)/', '$1$2', $mail);

如果user+@example.com也应该更换,你可以采取

preg_replace('/(.+)'+.*(@.+)/', '$1$2', $mail);

要在您的情况下使用 preg_replace,您可以执行以下操作:

$email = 'user+1@example.com';
$email = preg_replace("#(.+)'+.*@(.*)#", '$1@$2', $email);