开发者

fetch email ids from user input

开发者 https://www.devze.com 2023-02-08 06:00 出处:网络
i have a form where user can either input email ids like 开发者_如何学C someone@somewhere.com,anyone@anywhere.com

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

开发者_如何学C
someone@somewhere.com,anyone@anywhere.com

OR

someone@somewhere.com anyone@anywhere.com

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

I'm expecting some cool solutions.


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));


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,]+/", 
           "someone@somewhere.com,anyone@anywhere.com,",-1,PREG_SPLIT_NO_EMPTY);
print_r($keywords);
?>

OUTPUT

Array
(
    [0] => someone@somewhere.com
    [1] => anyone@anywhere.com
)

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


$input = "someone@somewhere.com,anyone@anywhere.com";
$emails = explode(",", $input);
if(count($emails) == 1) {
    $emails = explode(" ", $input);
}

print_r($emails);

/* output:

Array
(
    [0] => someone@somewhere.com
    [1] => anyone@anywhere.com
)

*/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号