PHP:文本框单独的电子邮件 [英] PHP: Textbox Separate Emails

查看:96
本文介绍了PHP:文本框单独的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何使用PHP分离每个电子邮件都是一个数组/对象?



是否可以给用户提供'或,或新行分开的选项? >

解决方案

如果给用户一个分隔符,可以使用 explode 。例如,使用;

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

您可以使用 \\\
(新行),而不是; 如果您愿意,



如果要根据所有划分字符串,请使用 preg_split

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

示例:

 code><?php 

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

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

print_r($ emails);
/ *
数组

[0] => test@test.com
[1] => test2@test2.com
[2] => email3@email3.com
[3] => email4@email4.com

* /
pre>

If I have a textbox where a user can enter multiple emails, i.e.

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

How can I use PHP to separate each email into an array/object?

Is it possible to give the users the option to separate by ';' or ',' or a new line?

解决方案

If you give the user a delimiter character, you can use explode. For instance, using ;:

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

You could use , or \n (new line) instead of ; if you preferred.

If you wanted to divide the string based on all these characters, use preg_split:

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

Example:

<?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
)
*/

这篇关于PHP:文本框单独的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆