PHP:Textbox单独的电子邮件

时间:2011-05-15 21:56:06

标签: php email

如果我有一个文本框,用户可以输入多封电子邮件,即

test@test.com
test2@test2.com
email3@email3.com

如何使用PHP将每封电子邮件分成数组/对象?

是否可以为用户提供以';'分隔的选项或','或新线?

3 个答案:

答案 0 :(得分:1)

如果您为用户提供分隔符,则可以使用explode。例如,使用;

$emails = explode(';', $_GET['emails']);

如果您愿意,可以使用,\n(新行)代替;

如果您想根据所有这些字符划分字符串,请使用preg_split

$emails = preg_split('/[;,\n]/', $_GET['emails']);

示例:

<?php

$emails = 'test@test.com;test2@test2.com
email3@email3.com,email4@email4.com';

$emails = preg_split('/[,;\n]/', $emails);

print_r($emails);
/*
Array
(
    [0] => test@test.com
    [1] => test2@test2.com
    [2] => email3@email3.com
    [3] => email4@email4.com
)
*/

答案 1 :(得分:1)

使用explode()返回一个字符串数组,每个字符串都是字符串的子字符串,通过在字符串分隔符形成的边界上将其拆分而形成。

array explode ( string $delimiter , string $string [, int $limit ] )

示例:

<?php
$str = 'one|two|three|four';

// positive limit
print_r(explode('|', $str, 2));

// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>

以上示例将输出:

Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)

更新

如果要按分隔符数组拆分,则需要使用preg_split()和相应的正则表达式。

答案 2 :(得分:0)

您可以使用空格或斜杠等分隔符,然后在PHP方面使用爆炸函数php.net/explode将其拆分。