我有一个像
这样的字符串<?php
$string = "
hello aaa@aaa.com , you are the best.
this email bbb@bbb.com my be fake
(i have question to ccc@ccc.com)
that's all";
?>
我想检测最后一封电子邮件(任何数量的电子邮件数量等于任何数字),例如:ccc@ccc.com
答案 0 :(得分:1)
<?php
function getLastEmail($_String)
{
$_RegVariables = "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i";
if (preg_match_all($_RegVariables, $_String, $_Matches))
{
$_Result = array();
$_RegResult = array_combine($_Matches[0], $_Matches[0]);
foreach($_RegResult as $key=>$value)
{
$_Result[] = $key;
}
}
return $_Result[ sizeOf( $_Result )-1 ];
}
$string = "
hello aaa@aaa.com , you are the best.
this email bbb@bbb.com my be fake
(i have question to ccc@ccc.com)
that's all";
$lastemail = getLastEmail($string);
echo $lastemail;
?>
现在$lastemail
是ccc@ccc.com
答案 1 :(得分:0)
这是一个快速解决方案,您可能需要执行一些字符串清理才能删除字符(例如'('或')'),但如果不存在,则可以使用。
<?php
$string = "
hello aaa@aaa.com , you are the best.
this email bbb@bbb.com my be fake
i have question to ccc@ccc.com
that's all";
$a = explode(' ', $string);
$aEmail = array();
foreach($a as $svar=>$sval)
{
if(preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $a[$svar]))
$aEmail[] .= $a[$svar];
}
$sEmail_Last = array_pop($aEmail);
echo $sEmail_Last; // echo ccc@ccc.com
?>