我正在尝试确定确定电子邮件地址是Outlook或Hotmail地址的最佳方式。
因此,我需要在@ 之后收集值即
testemail@outlook.com
捕获@
然而,这不会在所有实例中都起作用
此电子邮件地址有效:
"foo\@bar"@iana.org
我读到一个解决方案可能是爆炸,即:
$string = "user@domain.com";
$explode = explode("@",$string);
array_pop($explode);
$newstring = join('@', $explode);
echo $newstring;
这个解决方案似乎有点长,只能捕获第一个值
真的很感激一些帮助
答案 0 :(得分:0)
如果你爆炸了这个:
$string = "user@domain.com";
$explode = explode("@",$string);
它将是:
$explode[0] = user
$explode[1] = domain.com
答案 1 :(得分:0)
尝试使用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];
?>
答案 2 :(得分:0)
您可以随时保持简单,并使用strpos()或stripos()测试字符串中是否存在任何值。
if ( FALSE !== stripos($string, 'outlook') {
// outlook exists in the string
}
if ( FALSE !== stripos($string, 'hotmail') {
// hotmail exists in the string
}
答案 3 :(得分:0)
我建议与正则表达式匹配。
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.";
}
答案 4 :(得分:0)
我希望您能够轻松理解。
<?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
}
答案 5 :(得分:0)
试试这个:
$emailAddress = 'example\@sometext\@someothertext@hotmail.com';
$explodedEmail = explode('@', $emailAddress);
$emailServerHostName = end($explodedEmail);
$emailServerNameExploded = explode('.', $emailServerHostName);
$emailServerName = $emailServerNameExploded[0];
echo $emailServerName;