Reputation: 67
i have been trying to get the email address which has domains ends with .edu only using code below
$email = $_REQUEST['email'];
$school = substr($email, strpos($email, "@") + 1);
is there any way?
Upvotes: 0
Views: 265
Reputation: 796
This function works fine for me.
function isEmailEdu($email){
$pattern = '/@.*\.edu(\.[a-z]{2,})?$/';
if (preg_match($pattern, $email)) {
return true;
}
return false;
}
Upvotes: -1
Reputation: 163362
If .edu
is the last part of the email address, you could use strlen and substr:
$email = "[email protected]";
$end = ".edu";
$string_end = substr($email, strlen($email) - strlen($end));
if ($end === $string_end) {
// Ok
}
Maybe it is also an option to use explode and split on @
. Then use explode again and split on a dot and check if the array returned contains edu
:
$strings = [
"[email protected]",
"[email protected]",
"[email protected]"
];
foreach ($strings as $string) {
if (in_array("edu", explode(".", explode("@", $string)[1]))) {
// Etc..
}
}
Upvotes: 0
Reputation: 1692
It Should be work for get your domain name and domain extension:
$email = '[email protected]';
$getDomain = explode('@', $email);
$explValue = explode('.', $getDomain[1], 2);
print_r($explValue);
The out put is:
Array ( [0] => website [1] => edu )
After that you can check with
if($explValue[1] == 'edu'){
//your code here
}
Upvotes: 1
Reputation: 2738
You can use substr And get last 4 characters if this is valid as per your requirement so the email is valid else it not.
$string = "xyzasd.edu";
echo $txt = substr($string,-4);
if($txt == ".edu"){
//Valid
}else{
//Not Valid
}
Upvotes: -1
Reputation: 151
strpos($email, ".edu.");
it should be work.
for example [email protected]
Upvotes: -1
Reputation: 643
You just need to make a substring including the last 3 chars of the current string.
<?php
$tld = substr($email, strlen($email)-2, 3); // three last chars of the string
if ($tld = "edu") {
// do stuff
}
?>
Upvotes: 2