I have written a script (I'm quite new to PHP) which will take a list of elements as an input and checks whether it’s only a username or it contains username+domain name (I mean email).
If it’s only a username, then it will append '@test.com' into that username and combine all the strings together to send an email notification.
$recipients = array("kumar", "[email protected]", "ravi", "[email protected]");
$with_domain = array();
$without_domain = array();
$exp = "/^\S+@\S+\.\S+$/";
foreach ($recipients as $user) {
if(preg_match($exp, $user)) {
array_push($with_domain, $user);
} else {
array_push($without_domain, $user);
}
}
$without_domain_new = implode("@test.com", $without_domain)."@test.com";
print_r($with_domain);
print_r($without_domain_new);
echo "<br>";
$r = implode("", $with_domain);
$s = $without_domain_new.$r;
print "Email notification need to be sent to: $s";
Here script works as expected. But is the line $r = implode("", $with_domain); $s = $without_domain_new.$r; really required?
Is it be possible to merge array and strings in PHP? I mean, is it possible to merge $with_domain and $without_domain_new and store it to $s as a string?
Use:
It will give you an array with all email addresses you want.
And then you can use
implode('', $emails)or maybeimplode(',', $emails)to convert an array to a string for sending notification.