我正在尝试确定确定电子邮件地址是Outlook地址还是Hotmail地址的最佳方法。
因此,我需要收集@
即
testemail@outlook.com
将 @
但是,这并非在所有情况下都有效,因为
此电子邮件地址有效:
"foo'@bar"@iana.org
我读到一个解决方案可能是爆炸它,即:
$string = "user@domain.com";
$explode = explode("@",$string);
array_pop($explode);
$newstring = join('@', $explode);
echo $newstring;
此解决方案似乎有点长,仅捕获第一个值
真的很感激一些帮助
如果你爆炸这个:
$string = "user@domain.com";
$explode = explode("@",$string);
它将是:
$explode[0] = user
$explode[1] = domain.com
尝试使用 array_reverse() ti 选择电子邮件的最后一个值:
<?php
$email='exa@mple@hotmail.com';
$explode_email=explode('@',$email);
$reversed_array=array_reverse($explode_email);
$mailserver=explode('.',$reversed_array[0]);
echo $mailserver[0];
?>
你总是可以保持简单,并使用strpos()或stripos()测试字符串中是否存在任何一个值。
if ( FALSE !== stripos($string, 'outlook') {
// outlook exists in the string
}
if ( FALSE !== stripos($string, 'hotmail') {
// hotmail exists in the string
}
我希望这对你来说很容易理解。
<?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
}
我建议与正则表达式匹配。
if (preg_match("/'@hotmail.com$/", $email)) {
echo "on hotmail";
} else if (preg_match("/'@outlook.com$/", $email)) {
echo "on outlook";
} else {
echo "different domain";
}
此外,如果要将完整域捕获到变量,可以这样做:
$matches = [];
if (preg_match("/^.*'@(['w'.]+)$/", $email, $matches)) {
echo "Domain: " . $matches[1];
} else {
echo "not a valid email address.";
}
试试这个:
$emailAddress = 'example'@sometext'@someothertext@hotmail.com';
$explodedEmail = explode('@', $emailAddress);
$emailServerHostName = end($explodedEmail);
$emailServerNameExploded = explode('.', $emailServerHostName);
$emailServerName = $emailServerNameExploded[0];
echo $emailServerName;