Latox
Latox

Reputation: 4695

php -> foreach loop

I want to create a form, which is basically a text area which has e-mail addresses on new lines:

For example;

<textarea>
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected]
</textarea>

I then want this form to submit to a php page, which will split the e-mail addresses and put them into a loop, so I could paste a list of 300 e-mails into a text area, in that format, and then submit the form and it would do a foreach loop for EACH individual e-mail (e-mail them).

Could somebody please explain how I would split the e-mail addresses up individually and then process them into a loop?

Thanks a bunch

Upvotes: 0

Views: 274

Answers (2)

mdarwi
mdarwi

Reputation: 933

To get them into an array you can use PHP's explode function which splits a string up based on a string delimiter:

$email_addresses = explode(",\n", $email_address_string);
foreach ($email_addresses as $email_address) {
    // Process $email_address
}

Upvotes: 1

Phil
Phil

Reputation: 164736

If the email addresses are comma separated, use

$addresses = explode(',', $textareaValue);
foreach ($addresses as $address) {
    $address = trim($address); // Remove any extra whitespace
}

If you want to split the addresses on a number of different characters (comma, newline, space, etc) use preg_split in place of explode

$addresses = preg_split('/[\s,]+/', $textareaValue);

Upvotes: 4

Related Questions