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


If you blow this:

$string = "user@domain.com";

$explode = explode("@",$string);

      



It will be:

$explode[0] = user
$explode[1] = domain.com

      

0


source


try using array_reverse () ti fetch the last email value:



<?php
$email='exa@mple@hotmail.com';
$explode_email=explode('@',$email);
$reversed_array=array_reverse($explode_email);
$mailserver=explode('.',$reversed_array[0]);

echo $mailserver[0];
?>

      

0


source


You can always just keep it simple and check if any value exists in the string with strpos () or stripos ().

if ( FALSE !== stripos($string, 'outlook') {
    // outlook exists in the string
}

if ( FALSE !== stripos($string, 'hotmail') {
    // hotmail exists in the string
}

      

0


source


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


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


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







All Articles