How to get the latest email values โโafter @
I am trying to figure out the best way to determine if an email address is external or hot.
i therefore need to collect values โโafter @
ie
testemail@outlook.com
caputure @
however this will not work in all instances like
this email is valid:
"foo\@bar"@iana.org
I read that the solution might be to blow it up i.e.
$string = "user@domain.com";
$explode = explode("@",$string);
array_pop($explode);
$newstring = join('@', $explode);
echo $newstring;
this solution seems a bit long and only grabs the first values
would really appreciate some help
+3
source to share
6 answers
I would recommend regex matching.
if (preg_match("/\@hotmail.com$/", $email)) {
echo "on hotmail";
} else if (preg_match("/\@outlook.com$/", $email)) {
echo "on outlook";
} else {
echo "different domain";
}
Also, if you want to grab the full domain into a variable, you can do it like this:
$matches = [];
if (preg_match("/^.*\@([\w\.]+)$/", $email, $matches)) {
echo "Domain: " . $matches[1];
} else {
echo "not a valid email address.";
}
0
source to share
Hope it will be easy for you to understand.
<?php
$emailAddress = 'mailbox@hotmail.com'; //Email Address
$emailStringArray = explode('@',$emailAddress); // take apart the email string.
$host = $emailStringArray[1]; //last string after @ . $emailStringArray[0] = Mailbox & $emailStringArray[1] = host
if($host == "hotmail.com" || $host == "outlook.com"){
//matches to outlook.com or hotmail.com
}
else{
//Does not match to outlook.com or hotmail.com
}
0
source to share
Try the following:
$emailAddress = 'example\@sometext\@someothertext@hotmail.com';
$explodedEmail = explode('@', $emailAddress);
$emailServerHostName = end($explodedEmail);
$emailServerNameExploded = explode('.', $emailServerHostName);
$emailServerName = $emailServerNameExploded[0];
echo $emailServerName;
0
source to share