【发布时间】:2018-03-30 07:50:26
【问题描述】:
我一直在尝试仅使用以下代码获取域以 .edu 结尾的电子邮件地址
$email = $_REQUEST['email'];
$school = substr($email, strpos($email, "@") + 1);
有什么办法吗?
【问题讨论】:
标签: php email-validation
我一直在尝试仅使用以下代码获取域以 .edu 结尾的电子邮件地址
$email = $_REQUEST['email'];
$school = substr($email, strpos($email, "@") + 1);
有什么办法吗?
【问题讨论】:
标签: php email-validation
您只需要创建一个包含当前字符串最后 3 个字符的子字符串。
<?php
$tld = substr($email, strlen($email)-2, 3); // three last chars of the string
if ($tld = "edu") {
// do stuff
}
?>
【讨论】:
edu.pl tld。
它应该适用于获取您的域名和域扩展:
$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 和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..
}
}
【讨论】:
strpos($email, ".edu."); 应该可以了。
例如 gensek@metu.edu.tr
【讨论】:
您可以使用substr 并获取最后 4 个字符,如果这根据您的要求有效,则电子邮件有效,否则无效。
$string = "xyzasd.edu";
echo $txt = substr($string,-4);
if($txt == ".edu"){
//Valid
}else{
//Not Valid
}
【讨论】:
domains ends with .edu only 这个代码可以工作