Reputation: 1923
I have a form where i can get a list of emails separated by random characters: (some will use comma, others will use semi-colon or even *). Something like this:
[email protected];[email protected],[email protected]*[email protected]
Is there a way to explode the string with a regular expression?
In most cases, the users will type the same separator but i don't want to force people with an exclusive one.
Upvotes: 0
Views: 690
Reputation: 39704
For a complete solution you can use preg_match_all()
$emails = preg_match_all(
"/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
$emails,
$matches
);
$matches[0]
array will have all your emails.
Upvotes: 1
Reputation: 9158
See it in action:
(?<mail>[\w%+.-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6})
Upvotes: 0
Reputation: 46
You can use preg_split function.
Here is a quick example & output:
<?php
$str = "[email protected];[email protected],[email protected]*[email protected]";
$finalString=preg_split("/[*,;]/",$str);
var_dump($finalString);
?>
OUTPUT:
array
0 => string '[email protected]' (length=18)
1 => string '[email protected]' (length=18)
2 => string '[email protected]' (length=18)
3 => string '[email protected]' (length=18)
Upvotes: 1
Reputation: 72672
Something like this should get you started:
<?php
$test = preg_split ('/(;|,|\*)/', '[email protected];[email protected],[email protected]*[email protected]');
var_dump($test);
Upvotes: 3