i am trying to determine the best way to determine whether an email address is an outlook or hotmail address.
i therefore need to collect the values after the @
i.e
testemail@outlook.com
caputure the @
however this will not work in all instance as
this email address is valid:
"foo\@bar"@iana.org
i read that a solution could be to explode it, i.e:
$string = "user@domain.com";
$explode = explode("@",$string);
array_pop($explode);
$newstring = join('@', $explode);
echo $newstring;
this solution seems bit long and only captures the first values
would really appreciate some help
if You explode This :
$string = "user@domain.com";
$explode = explode("@",$string);
it Will be :
$explode[0] = user
$explode[1] = domain.com
try to use array_reverse() ti pick the last value of email:
<?php
$email='exa@mple@hotmail.com';
$explode_email=explode('@',$email);
$reversed_array=array_reverse($explode_email);
$mailserver=explode('.',$reversed_array[0]);
echo $mailserver[0];
?>
You could always just keep it simple and test if either value exists in the string using strpos() or stripos().
if ( FALSE !== stripos($string, 'outlook') {
// outlook exists in the string
}
if ( FALSE !== stripos($string, 'hotmail') {
// hotmail exists in the string
}
I would recommend matching with a regular expression.
if (preg_match("/\@hotmail.com$/", $email)) {
echo "on hotmail";
} else if (preg_match("/\@outlook.com$/", $email)) {
echo "on outlook";
} else {
echo "different domain";
}
Additionally, if you want to capture full domain to 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.";
}
I hope this 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
}
Try this :
$emailAddress = 'example\@sometext\@someothertext@hotmail.com';
$explodedEmail = explode('@', $emailAddress);
$emailServerHostName = end($explodedEmail);
$emailServerNameExploded = explode('.', $emailServerHostName);
$emailServerName = $emailServerNameExploded[0];
echo $emailServerName;