The PHP website created by its users!
Home
»
PHP » Comma Delimited Email Collection
|
Comma Delimited Email Collection
| Author | Ovid Burke |
| Description | This function searches a data source for email addresses and create a comma delimited of any addresses found. |
| Rating | (1 votes) |
PHP Code
<?php
// function to make comma delimited list od email addresses found within source data
function collect_emails($source_data) {
// mark all email address found within the source data
$source_data = preg_replace("/([_a-zA-Z0-9-]+)(.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(.[a-zA-Z0-9-]+)*(.[a-zA-Z]{2,4})/", '<email>\0</email>', $source_data);
// find all marked emails and pass them to an array
@preg_match_all("/<email>(.*?)</email>/is", $source_data, $matches);
for($i = 0; $i < count($matches); $i++) {
$email_array = $matches[$i];
}
// loop through the matches and pass them to comma delimited list
$i = 0;
while($i < count($email_array)) {
$comma_list .= $email_array[$i] . ', ';
$i++;
}
// return comma delimited list after stripping away the last comma
$comma_list = rtrim($comma_list, ', ');
return $comma_list;
}
// source data
$source_data = 'Your source data can be written here, and contain any number of email addresses like mail@yourdomain.com or infor@yourdomain.com or name@otherdomain.com.';
// here is my implementation of the function
echo collect_emails($source_data);
?>
Comments
No comments posted yet.
|