自动更改电子邮件地址的拼写错误


Automatically change a typo error of an email address

我试图制作一个litte php脚本,当在表单中插入电子邮件地址时,该脚本将自动更正拼写错误。

if (strpos($_POST["email"], "@hotmail.comm") !== false) {
    $_POST["email"] = str_replace('@hotmail.comm', '@hotmail.com', $_POST["email"]);
    goto end;
}
if (strpos($_POST["email"], "@homail.com") !== false) {
    $_POST["email"] = str_replace('@homail.com', '@hotmail.com', $_POST["email"]);
    goto end;
}
end:

当我测试它时,@homail.com一切正常,但当我使用@hotmail.comm测试时,电子邮件地址已更改为@hotmail.com.cn

你知道@hotmail.comm哪里出了问题吗?

问候,

Arie

我觉得不错,但goto,真的吗?分配给$_POST?不

你可以写得更短更好:

$improvements = array('@hotmail.comm' => '@hotmail.com',
                      '@homail.com'   => '@hotmail.com');
$emailAddress = str_replace(array_keys($improvements),
                            array_values($improvements),
                            $_POST["email"]);

显然,该列表可以在不添加太多代码的情况下进行扩展。始终尝试编写易于维护的代码。

@Arie,请检查下面的代码以获得您的解决方案。

$email = $_POST["email"];
if (strpos($email, "@hotmail.comm") !== false) { 
    $_POST["email"] = str_replace('@hotmail.comm', '@hotmail.com', $email);
}
if (strpos($email, "@homail.com") !== false) {
    $_POST["email"] = str_replace('@homail.com', '@hotmail.com', $email);
}
echo $_POST["email"];die; 
相关文章: