【发布时间】:2020-11-01 22:56:39
【问题描述】:
我创建了一个简短的脚本,它应该查看 $email 并查看是否有 @ 符号并告诉您有没有。然后,它应该检查每个扩展 $protocols 数组并告诉您 $email 中是否有扩展。它通过了前两个 $protocols;但是,在没有错误消息的情况下停止或继续执行 $protocols。
<?
// Set searching info!
$attsymbol = "@";
$protocols = array('.com', '.net', '.org', '.biz', '.info', '.edu', '.mil', '.cc', '.co', '.website', '.site', '.tech', '.tv');
// Set email
$email = "bob@email.website";
// check for the @ symbol!
if (!strpos($email, $attsymbol))
{
die ("There is no " . $attsymbol . " in the email address!<br>");
}
else
{
echo "This is an " . $attsymbol . " in the email address!<br>";
// Check for all of the protocols in the array!
foreach ($protocols as $protocol)
{
echo $protocol . "<br>";
if (!strpos($email, $protocol))
{
die("There is no " . $protocol . " in the email address!<br>");
}
else
{
echo"There is a " . $protocol . " in the email address!<br>";
}
}
}
?>
提前感谢您为此提供的帮助!
【问题讨论】:
-
die导致您的程序在电子邮件地址中不存在的第一个protocol处退出。 -
.com、.net 等不是协议.. 你为什么不使用
filter_var($email, FILTER_VALIDATE_EMAIL)?就像@@email.website@@这样的东西会通过你的检查 -
感谢模具建议。我会马上改变。至于 filter_var 的建议,我已经做了一些初步的研究,只得到了关于它的使用的简短说明。您能否提供一个如何编写和使用它的示例?
-
这能回答你的问题吗? How to validate an email address in PHP