Reputation: 2022
I have this code in php:
<?php
$mails_list = "[email protected], User 2 <[email protected]>, User 3 ([email protected])";
$mails_arr = explode(',', $mails_list);
foreach($mails_arr as $mail){
echo "{$mail}<br>";
}
?>
Note that with that code he output will be:
[email protected]
User 2 <[email protected]>
User 3 ([email protected])
And I dont want that. What I want is that the output must be:
[email protected]
[email protected]
[email protected]
How can I do that with PHP?
Upvotes: 0
Views: 37
Reputation: 4519
Here you go,
<?php
$mails_list = "[email protected], User 2 <[email protected]>, User 3 ([email protected])";
$mails_arr = explode(',', $mails_list);
preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $mails_list, $found_mails);
foreach ($found_mails[0] as $each) { echo $each.'<br>'; }
Upvotes: 2
Reputation: 76636
You can use preg_match_all()
to capture all the emails with a regex:
preg_match_all('/[<(]?(\w+@\w+\.\w+)[>)]?/', $mails_list, $matches);
echo implode("\n", $matches[1]);
Upvotes: 2