如何生成有效的长电子邮件地址?
How do I generate long valid email address?
我的意思是:
$ 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)
The format of email addresses is local-part@domain where the local part may be up to 64 characters long and the domain may have a maximum of 255 characters
要生成一些长字符串,您可以使用像 str_repeat
这样简单的东西
var_dump(str_repeat('a', 64).'@'.str_repeat('g', 255).'.com');
You may use this Function to generate random email addresses conforming to a maximum of 64 characters Local-Part and a maximum of 255 characters Domain Part.
<?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());
引用 the link Justinas 提供的更多内容:
The format of email addresses is local-part@domain where the local part may be up to 64 characters long and the domain may have a maximum of 255 characters—but the maximum of 256-character length of a forward or reverse path restricts the entire email address to be no more than 254 characters long.
现在,如果我们查看 filter_var
的 source,我们会发现那里基本上有两个检查:
- 如果整个电子邮件地址是 longer than 320 symbols
- 如果匹配the regex(除非我们指定
FILTER_FLAG_EMAIL_UNICODE
标志)
正则表达式可以改写如下:
/^
### check if total length is no more than 254 symbols
(?!
(?:
(?:\x22?\x5C[\x00-\x7E]\x22?)
|
(?:\x22?[^\x5C\x22]\x22?)
){255,}
)
### check if the part before @ is no more than 64 symbols
(?!
(?:
(?:\x22?\x5C[\x00-\x7E]\x22?)
|
(?:\x22?[^\x5C\x22]\x22?)
){65,}
@
)
(?:
(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)
|
(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)
)
(?:
\.
(?:
(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)
|
(?:
\x22
(?:
[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\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)
我的意思是:
$ 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)
The format of email addresses is local-part@domain where the local part may be up to 64 characters long and the domain may have a maximum of 255 characters
要生成一些长字符串,您可以使用像 str_repeat
var_dump(str_repeat('a', 64).'@'.str_repeat('g', 255).'.com');
You may use this Function to generate random email addresses conforming to a maximum of 64 characters Local-Part and a maximum of 255 characters Domain Part.
<?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());
引用 the link Justinas 提供的更多内容:
The format of email addresses is local-part@domain where the local part may be up to 64 characters long and the domain may have a maximum of 255 characters—but the maximum of 256-character length of a forward or reverse path restricts the entire email address to be no more than 254 characters long.
现在,如果我们查看 filter_var
的 source,我们会发现那里基本上有两个检查:
- 如果整个电子邮件地址是 longer than 320 symbols
- 如果匹配the regex(除非我们指定
FILTER_FLAG_EMAIL_UNICODE
标志)
正则表达式可以改写如下:
/^
### check if total length is no more than 254 symbols
(?!
(?:
(?:\x22?\x5C[\x00-\x7E]\x22?)
|
(?:\x22?[^\x5C\x22]\x22?)
){255,}
)
### check if the part before @ is no more than 64 symbols
(?!
(?:
(?:\x22?\x5C[\x00-\x7E]\x22?)
|
(?:\x22?[^\x5C\x22]\x22?)
){65,}
@
)
(?:
(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)
|
(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)
)
(?:
\.
(?:
(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)
|
(?:
\x22
(?:
[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\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)