【问题标题】:Email validation with edu domains only仅使用 edu 域的电子邮件验证
【发布时间】:2018-03-30 07:50:26
【问题描述】:

我一直在尝试仅使用以下代码获取域以 .edu 结尾的电子邮件地址

$email = $_REQUEST['email'];

$school = substr($email, strpos($email, "@") + 1);    

有什么办法吗?

【问题讨论】:

    标签: php email-validation


    【解决方案1】:

    您只需要创建一个包含当前字符串最后 3 个字符的子字符串。

    <?php
    $tld = substr($email, strlen($email)-2, 3);    // three last chars of the string
    if ($tld = "edu") {
        // do stuff
    }
    ?>
    

    【讨论】:

    • 例如,它不适用于波兰的edu.pl tld。
    • 当然,我认为他正在寻找 .edu 顶级域(问题中未指定)。当然,代码必须适应任何自定义情况:)
    【解决方案2】:

    它应该适用于获取您的域名和域扩展:

     $email = 'test@website.edu';
    $getDomain = explode('@', $email);
    $explValue = explode('.', $getDomain[1], 2);
    print_r($explValue);
    

    输出是:

    Array ( [0] => website [1] => edu )
    

    之后你可以检查

    if($explValue[1] == 'edu'){
    //your code here
    }
    

    【讨论】:

      【解决方案3】:

      如果.edu 是电子邮件地址的最后一部分,您可以使用strlensubstr

      $email = "test@test.edu";
      $end = ".edu";
      $string_end = substr($email, strlen($email) - strlen($end));
      if ($end === $string_end) {
          // Ok
      }
      

      也许使用explode 并在@ 上拆分也是一种选择。然后再次使用explode并在一个点上拆分并检查返回的数组是否包含edu

      $strings = [
          "test@test.edu",
          "test@test.edu.pl",    
          "test@test.com"
      ];
      foreach ($strings as $string) {
          if (in_array("edu", explode(".", explode("@", $string)[1]))) {
              // Etc..
          }
      }
      

      Demo

      【讨论】:

        【解决方案4】:

        strpos($email, ".edu."); 应该可以了。 例如 gensek@metu.edu.tr

        【讨论】:

          【解决方案5】:

          您可以使用substr 并获取最后 4 个字符,如果这根据您的要求有效,则电子邮件有效,否则无效。

          $string = "xyzasd.edu";
          echo $txt = substr($string,-4);
          
          if($txt == ".edu"){
              //Valid
          }else{
              //Not Valid
          }
          

          【讨论】:

          • domains ends with .edu only 这个代码可以工作
          猜你喜欢
          • 2012-01-30
          • 2011-01-31
          • 1970-01-01
          • 2011-11-02
          • 1970-01-01
          • 1970-01-01
          • 2016-10-24
          • 2016-05-12
          • 2017-06-23
          相关资源
          最近更新 更多