Harish
Harish

Reputation: 2324

fetch email ids from user input

i have a form where user can either input email ids like

[email protected],[email protected]

OR

[email protected] [email protected]

I'm using PHP for scripting i would like to have the email ids extracted!

I'm expecting some cool solutions.

Upvotes: 0

Views: 99

Answers (3)

Harish
Harish

Reputation: 2324

Hi guys thanks for your patient and speedy replies but i found out a way i'll accept this only when you confirm it has no bugs.ie no replies for a day

<?php
$keywords = preg_split("/[\s,]+/", 
           "[email protected],[email protected],",-1,PREG_SPLIT_NO_EMPTY);
print_r($keywords);
?>

OUTPUT

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

it was omitting even the extra comma at the end. even if i added more space waiting for your replies!

Upvotes: 2

Christian
Christian

Reputation: 19740

You could use preg_split(), but to be honest, the simplest solution would be to convert all spaces to commas (or vice versa) and then explode by that.

$emails = explode(",", str_replace(" ", ",", $email_string));

Upvotes: 2

Alec Gorge
Alec Gorge

Reputation: 17390

$input = "[email protected],[email protected]";
$emails = explode(",", $input);
if(count($emails) == 1) {
    $emails = explode(" ", $input);
}

print_r($emails);

/* output:

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

*/

Upvotes: 1

Related Questions