简体   繁体   中英

Separating Multiple Email Addresses in PHP

Following this thread:

PHP: Textbox Separate Emails

I am attempting to separate multiple email addresses that are being entered into a textbox. My problem is that if I use the advice from above:

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

It works great only if there is no space after the delimiter. How can I adjust this so that someone can separate email addresses by either just the delimiter or the delimiter plus a space.

eg. ( email1@xyz.com;email2@xyz.com ) AND ( email1@xyz.com; email2@xyz.com ) should both work but currently only the first example works.

I'd like to improve Michael Berkowski's answer by making the spaces optional:

$emails = preg_split('/\s*[;,\n]\s*/', $_POST['email_receivers']);

\\s+ requires one or more spaces. \\s* makes them optional (zero or more)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM