从用户输入中获取电子邮件ID

时间:2011-02-04 04:00:33

标签: php email

我有一个表单,用户可以输入电子邮件ID,如

someone@somewhere.com,anyone@anywhere.com

OR

someone@somewhere.com anyone@anywhere.com

我正在使用PHP编写脚本我想要提取电子邮件ID!

我期待一些很酷的解决方案。

3 个答案:

答案 0 :(得分:2)

您可以使用preg_split(),但说实话,最简单的解决方案是将所有空格转换为逗号(反之亦然),然后将其爆炸。

$emails = explode(",", str_replace(" ", ",", $email_string));

答案 1 :(得分:2)

嗨,大家好,感谢您的耐心和快速回复,但是 我发现了一种方法,只有当你确认它没有错误时才接受这个。我没有回复一天

<?php
$keywords = preg_split("/[\s,]+/", 
           "someone@somewhere.com,anyone@anywhere.com,",-1,PREG_SPLIT_NO_EMPTY);
print_r($keywords);
?>

输出

Array
(
    [0] => someone@somewhere.com
    [1] => anyone@anywhere.com
)

最后甚至省略了额外的逗号。即使我增加了更多的空间 等待你的回复!

答案 2 :(得分:1)

$input = "someone@somewhere.com,anyone@anywhere.com";
$emails = explode(",", $input);
if(count($emails) == 1) {
    $emails = explode(" ", $input);
}

print_r($emails);

/* output:

Array
(
    [0] => someone@somewhere.com
    [1] => anyone@anywhere.com
)

*/