仅使用 edu 域的电子邮件验证

Email validation with edu domains only

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

$email = $_REQUEST['email'];

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

有什么办法吗?

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

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

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

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

$string = "xyzasd.edu";
echo $txt = substr($string,-4);

if($txt == ".edu"){
    //Valid
}else{
    //Not Valid
}

获取您的域名和域名后缀应该可行:

 $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
}

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

$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