1

I have a .txt list with a large number of birthdays, in a format like this:

1975-12-13|Amy Lee|amylee@gmail.com

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!

  • 3
    In `$message=` ... the `=` overwrites the variable each time, so I expect you'll only ever see the last value it found. Try concatenating instead - i.e. `$message .=`... – ADyson May 26 '23 at 09:29
  • P.S. Also it's unclear why you're concatenating empty strings (`''`) either side of each name. That doesn't do anything useful. You're just adding literally nothing to the string! – ADyson May 26 '23 at 09:39
  • Basically this is a duplicate of [How can I combine two strings together in PHP?](https://stackoverflow.com/questions/8336858/how-can-i-combine-two-strings-together-in-php), specifically – ADyson May 26 '23 at 09:40

2 Answers2

0

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).

$f=file('birthday.txt');
$today=date('m-d');
$message = '';
for ($i=0; $i<count($f); $i++) {
    $info=explode ('|',$f[$i]);
    if (substr($info[0],5)==$today) {
        $firstlastname= $info[1];
        //concat each name with a new line and remove the initial empty string, 
       //you can use commas instead of new lines as well
        $message .= $firstlastname.'<br/>';
    }
}

echo "These people have a birthday today: ".$message;
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

My answer makes use of 'foreach' and regex. It also add their names to an array, identified by their email addresses.

<?php    
// Today's date
$today = date('Y-m-d');
// Array of birthdays
$birthdays = [];
if (file_exists('./birthday.txt')) {
    foreach (file('birthday.txt') as $entry) {
        /*
        Check for people with their birthdays today
         */
        if (preg_match('/^' . $today . '\|([\w ]+)\|(.+)$/u', $entry, $matches)) {
            /*
            Let's put their names in $birthdays
           email => Name
            */
            $birthdays[$matches[2]] = $matches[1];
        }
    }
}

// To list their names, you can do:
if (!empty($birthdays)) {
    $names = implode(', ', $birthdays);
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '23 at 16:25