skip to Main Content

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


  1. 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;
    
    Login or Signup to reply.
  2. 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.

    <?php
    // Make sure we're in the right directory
    $dir = getcwd();
    chdir(__DIR__);
    // Today's date
    $today = date('Y-m-d');
    // Array of birthdays
    $birthdays = [];
    // Message
    $msg = '';
    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];
            }
        }
    }
    
    // Check if there are birthdays today
    if (!empty($birthdays)) {
        /*
        A message
        */
        $msg = 'The following people are celebrating their bithdays today:' . PHP_EOL;
        $names = implode(', ', $birthdays); // their names
        $msg .= $names . PHP_EOL.PHP_EOL; // add their names to the $msg
    
        /*
        Optional,
        Let's wish them a happy birthday
        You can do any other thing by looping through the $birthdays
        */
        $subject = 'Happy birthday!';
        foreach ($birthdays as $email => $name) {
            $message = "Hello $name, enjoy this special day in celebration of a most wonderful you.nnIts your friend,n Joe";
            $message = wordwrap($message, 70);
            if (@mail($email, $subject, $message)) {
                /*
                Mail sent, lets remove the entry */
                unset($birthdays[$email]);
            }
        }
        /*
        See if some mails weren't delivered
        */
        if (!empty($birthdays)) {
            $error_msg = 'Failed to send mails to the following persons:' . PHP_EOL;
            foreach ($birthdays as $email => $name) {
                $error_msg = $error_msg . "$name ($email), "; // the short form wasn't working, maybe its because of the errors triggered by msmtp.
            }
            $error_msg = rtrim($error_msg, ', ');
        }
    }
    
    echo($msg);
    if (isset($error_msg ) {
        echo($error_msg);
    }
    
    // Set back previous directory
    chdir($dir);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search