Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yetanotherdev267/f2776518093f42543eae to your computer and use it in GitHub Desktop.
Save yetanotherdev267/f2776518093f42543eae to your computer and use it in GitHub Desktop.
Extract email list from Outlook or Gmail address string
<?php
function extract_emails($string) {
$separator = ';';
if(!stripos($string, ';') && stripos($string, ','))
$separator = ',';
$list = explode($separator, $string);
$email_list = array();
foreach($list as $item) {
$item = trim($item);
$start = 0;
$length = strlen($item);
if($length==0 || !stripos($item, '@'))
continue;
$start_bracket = stripos($item, '<');
$end_bracket = stripos($item, '>');
if($start_bracket && $end_bracket) {
$start = $start_bracket + 1;
$length = $end_bracket - $start_bracket - 1;
}
$email = trim(substr($item, $start, $length));
$email_list[] = strtolower($email);
}
return $email_list;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment