I have a .txt list with a large number of birthdays, in a format like this:
1975-12-13|Amy Lee|[email protected]
I would like to create php code that would go through the whole list, find persons who have a birthday today, and list their names.
What I manage to do is this, but it is showing only one name, even tho there’s multiple birthdays at the same day:
$f=file('birthday.txt');
$today=date('m-d');
for ($i=0; $i<count($f); $i++) {
$info=explode ('|',$f[$i]);
if (substr($info[0],5)==$today) {
$firstlastname= $info[1];
$message=''.$firstlastname.'';
}
}
return ''.$message.'';
I guess that I should use foreach there somehow, I’m not a pro in PHP obviously, but I don’t know how to do that. Any suggestions please?
Thanks!
2
Answers
Your current code is ok, the only thing is you are overwriting the
$message
variable inside the loop, hence you will get only one name at the end.To fix this use the
.
operator along with the line break (just to show data in an appropriate way).Here’s an alternative using foreach and regex. It also add their names to an array, identified by their email addresses. NB: Two people can have the same names.