0

I would like to create a e-form, the user can input more than one email address on textarea field.

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>    
 <textarea  name="email"></textarea>
<input type="submit" name="submit" value="Submit">      
</form>    

When user type more than one email on the textarea.like the following image enter image description here

 <?php    

echo $_POST["name"].'<br>';
echo $_POST["email"].'<br>'; //make the three email convert to three variable.
?>

any idea do do this???

foreach ($_POST["email"] as $email) {
     // anyidea
    }
 $_POST["email"][0] = '[email protected]';
 $_POST["email"][1] = '[email protected]';
 $_POST["email"][2] = '[email protected]';

Thank you very much .

1

3 Answers 3

3

This is what I would do,

  $emails = array_filter(array_map('trim', explode(',', $_POST['email'])));

Explode breaks a string into an array based on the first argument, array_map, works kind of like a loop and applies trim to each element, which trims white space (removes empty spaces from both sides). Array filter removes any array elements that are falsy. Such as '' empty strings.

So this takes care of things like

 [email protected], ,,[email protected]

Output

array(
   '[email protected]',
   '[email protected]'
)

if you want to be really flexible do this

  $emails = array_filter(array_map('trim', preg_split('/(,|\||\s)/', $_POST['email'])))

Which does the same as above but lets you use spaces commas or pipes as the delimiters.

Sign up to request clarification or add additional context in comments.

Comments

1

You may try splitting your CSV list of emails on the pattern \s*,\s*. This will handle any amount of whitespace coming before or after the comma separators.

$input = "[email protected], [email protected] , [email protected]";
$emails = preg_split('/\s*,\s*/', $input);
print_r($emails);

Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] )

Comments

0

You can use explode function in PHP http://php.net/manual/en/function.explode.php

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.