我如何生成长的有效电子邮件地址?

我的意思是:

$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@gmail.com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
string(74) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@gmail.com"
$ php -r 'var_dump(filter_var(str_repeat("a", 65) . "@gmail.com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
bool(false)

邮箱名称的长度是否有限制? 我需要长度超过255个字符的电子邮件(用于测试)。

UPD让我解释一下我需要这个。 我想确保在我的测试中用户不能指定超过255个字符的电子邮件地址。 这就是为什么我需要一个长的电子邮件地址。

UPD继Justinas建议:

$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat("g", 63) . ".com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
string(132) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg.com"
$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat("g", 64) . ".com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
bool(false)

资源

电子邮件地址的格式是本地部分@域, 本地部分可能长达64个字符可能最多255个字符


要生成一些长字符串,你可以使用像str_repeat这样简单的东西

var_dump(str_repeat('a', 64).'@'.str_repeat('g', 255).'.com');

您可以使用此功能来生成符合最多64个字符本地部分和最多255个字符域部分的随机电子邮件地址。

     <?php

        function generateEmailAddress($maxLenLocal=64, $maxLenDomain=255){
            $numeric        =  '0123456789';
            $alphabetic     = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
            $extras         = '.-_';
            $all            = $numeric . $alphabetic . $extras;
            $alphaNumeric   = $alphabetic . $numeric;
            $alphaNumericP  = $alphabetic . $numeric . "-";
            $randomString   = '';

            // GENERATE 1ST 4 CHARACTERS OF THE LOCAL-PART
            for ($i = 0; $i < 4; $i++) {
                $randomString .= $alphabetic[rand(0, strlen($alphabetic) - 1)];
            }
            // GENERATE A NUMBER BETWEEN 20 & 60
            $rndNum         = rand(20, $maxLenLocal-4);

            for ($i = 0; $i < $rndNum; $i++) {
                $randomString .= $all[rand(0, strlen($all) - 1)];
            }

            // ADD AN @ SYMBOL...
            $randomString .= "@";

            // GENERATE DOMAIN NAME - INITIAL 3 CHARS:
            for ($i = 0; $i < 3; $i++) {
                $randomString .= $alphabetic[rand(0, strlen($alphabetic) - 1)];
            }

            // GENERATE A NUMBER BETWEEN 15 & $maxLenDomain-7
            $rndNum2        = rand(15, $maxLenDomain-7);
            for ($i = 0; $i < $rndNum2; $i++) {
                $randomString .= $all[rand(0, strlen($all) - 1)];
            }
            // ADD AN DOT . SYMBOL...
            $randomString .= ".";

            // GENERATE TLD: 4
            for ($i = 0; $i < 4; $i++) {
                $randomString .= $alphaNumeric[rand(0, strlen($alphaNumeric) - 1)];
            }

            return $randomString;

        }


        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());

从Justinas的链接中引用更多:

电子邮件地址的格式是本地部分@域,其中本地部分 最长可达64个字符 最多可以有255个字符,但正向或反向路径的最大长度为256个字符限制整个域电子邮件地址 不超过254个字符

现在,如果我们看一下filter_var的来源,我们会看到基本上有两个检查:

  • 如果整个电子邮件地址超过320个符号
  • 如果它匹配正则表达式(除非我们指定FILTER_FLAG_EMAIL_UNICODE标志)
  • 正则表达式可以改写如下:

    /^
      ### check if total length is no more than 254 symbols
      (?!
        (?:
          (?:x22?x5C[x00-x7E]x22?)
          |
          (?:x22?[^x5Cx22]x22?)
        ){255,}
      )
      ### check if the part before @ is no more than 64 symbols
      (?!
        (?:
          (?:x22?x5C[x00-x7E]x22?)
          |
          (?:x22?[^x5Cx22]x22?)
        ){65,}
        @
      )
      (?:
        (?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)
        |
        (?:x22(?:[x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]|(?:x5C[x00-x7F]))*x22)
      )
      (?:
        .
        (?:
          (?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)
          |
          (?:
            x22
            (?:
              [x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]
              |
              (?:x5C[x00-x7F])
            )*
            x22
          )
        )
      )*
      @
      (?:
        (?:
          ### no segment must be longer than 64 symbols
          (?!.*[^.]{64,})
          (?:
            (?:
              (?:xn--)?
              [a-z0-9]+
              (?:-+[a-z0-9]+)*
              .
            ){1,126}
          ){1,}
          (?:
            (?:[a-z][a-z0-9]*)
            |
            (?:(?:xn--)[a-z0-9]+)
          )
          (?:-+[a-z0-9]+)*
        )
        |
        ### ip
        (?:
          [
          (?:
            (?:
              IPv6:
              (?:
                (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})
                |
                (?:
                  (?!(?:.*[a-f0-9][:]]){7,})
                  (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?
                  ::
                  (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?
                )
              )
            )
            |
            (?:
              (?:
                IPv6:
                (?:
                  (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)
                  |
                  (?:
                    (?!(?:.*[a-f0-9]:){5,})
                    (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?
                    ::
                    (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?
                  )
                )
              )?
              (?:
                (?:25[0-5])
                |
                (?:2[0-4][0-9])
                |
                (?:1[0-9]{2})
                |
                (?:[1-9]?[0-9])
              )
              (?:
                .
                (?:
                  (?:25[0-5])
                  |
                  (?:2[0-4][0-9])
                  |
                  (?:1[0-9]{2})
                  |
                  (?:[1-9]?[0-9])
                )
              ){3}
            )
          )
          ]
        )
      )
    $/iD
    

    知道我们能得到的最好的是:

    $ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat(str_repeat("g", 63) . ".", 2) . str_repeat("g", 61), FILTER_VALIDATE_EMAIL));'
    Command line code:1:
    string(254) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg.ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg.ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
    $ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat(str_repeat("g", 63) . ".", 2) . str_repeat("g", 62), FILTER_VALIDATE_EMAIL));'
    Command line code:1:
    bool(false)
    
    链接地址: http://www.djcxy.com/p/92919.html

    上一篇: How do I generate long valid email address?

    下一篇: What is the maximum length of a display name in an email address