【问题标题】:Understanding foreach with arrays用数组理解 foreach
【发布时间】: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

标签: php arrays foreach strpos


【解决方案1】:

相反,您可以使用FILTER_VALIDATE_EMAIL 来检查该值是否为有效的电子邮件地址。

<?php

$email = "bob@email.website";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "<pre>Valid</pre>";
} else {
    echo "<pre>Not Valid</pre>";
}

【讨论】:

  • @MattKomarnicki 这里没有链接。这是问题的答案。
【解决方案2】:

你想循环并寻找你的针。

在下面的循环中,如果找到针,我设置一个标志并打破循环。

我正在反转 tlds 和电子邮件以进行比较,因为这样比较字符串的结尾更容易。

示例输出用于三个给定的电子邮件输入。

foreach(['jimbob', 'bob@example.uk', 'bob@email.website'] as $email) {
    $errors = [];
    $tlds   = ['.com', '.net', '.org', '.biz', '.info', '.edu', '.mil', '.cc', '.co', '.website', '.site', '.tech', '.tv'];

    // @ check.
    if (strpos($email, '@') === false) {
        $errors[] = 'No @ in address.';
    }

    // tld check.
    $reversed_tlds  = array_map('strrev', $tlds);
    $reversed_email = strrev($email);
    $found = false;
    foreach($reversed_tlds as $reverse_tld) {
        if(strpos($reversed_email, $reverse_tld) === 0) {
            $found = true;
            break;
        }
    }
    if(!$found) {
        $errors[] = 'Email address must end in one of: ' . implode(',', $tlds);
    }

    var_dump($errors);
}

输出:

array(2) {
  [0]=>
  string(16) "No @ in address."
  [1]=>
  string(102) "Email address must end in one of: .com,.net,.org,.biz,.info,.edu,.mil,.cc,.co,.website,.site,.tech,.tv"
}
array(1) {
  [0]=>
  string(102) "Email address must end in one of: .com,.net,.org,.biz,.info,.edu,.mil,.cc,.co,.website,.site,.tech,.tv"
}
array(0) {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多