Why is php strpos to determine if text is contained in another string not working?

39 Views Asked by At

Here is my Code:

                $donor_comment_string = "BEN_W,NICOLA_M";

                if (strpos($donor_comment_string, 'NICOLA_M') != false) {
                echo 'TESTNicola';
                }
                                    if (strpos($donor_comment_string, 'BEN_W') != false) {
                echo 'TESTBen';
                }

I am using the above code which is Returning only TESTNicola.

I use the same function later on in a loop, checking it against an array of the Codes such as BEN_W and NICOLA_M and it is working down there.

Here is my real code minus the email.

                                                     $donor_comment_string = "BEN_W,NICOLA_M";

                $emails_array = array(
    array("Nicola", "ben@****.com", "Multi Campaign Donation Tester", "6666", "NICOLA_M"),
    array("Ben", "ben@****.com", "Multi Campaign Donation Tester", "0000", "BEN_W"),
    array("Test Name", "ben@****.", "Multi Campaign Donation Tester", "9999", "TEST_NOT"),
    
);
                for ($row = 0; $row < count($emails_array); $row++) {
                    if (strpos($donor_comment_string, $emails_array[$row][4], 0) != false) {
                        $participant_email = $emails_array[$row][1];
                        $participant_password = $emails_array[$row][3];
                        $campaign_name_string = "Campaign: " . $emails_array[$row][2];
                        $participant_name = $emails_array[$row][0];

                        $to = $participant_email;
                        $message_string = '';
                        $form_name = str_replace(" ", "_", "Marthon Donate To Multiple Participants");

                        if (isset($_POST[$form_name . $i])) {
                            $message_string = $_POST[$form_name . $i];
                        }
                        if ($message_string != "") {
                            $message_string = "\n\n" . $message_string;
                        }
                        if ($donor_comment_string != "") {
                            $donor_comment_string = "\nDonor Comment: " . $donor_comment_string;
                        }
                        //if ($message_string != "" && isset($_POST['date'.$i]) && $_POST['date'.$i] == $date.$i) {}

                        $msg = 'Hi ' . $participant_name . ",\n\n" .
                                'You have a new sponsor! ' . "\n\nDonor Name: " . $donor_name_string . "\nDonor Email: " . $donor_email_string . "\nAmount: " . $donor_amount_string . $message_string . "\n\nPlease feel free to email me if you have any questions. \nBen";
                        $msg_to_print = 'Hi ' . $participant_name . ',<br/><br/>' .
                                'You have a new sponsor: ' . "<br/><br/>Donor Name: " . $donor_name_string . "<br/>Donor Email: " . $donor_email_string . "<br/>Amount: " . $donor_amount_string . "<br/><br/>" . $message_string . "<br/><br/>Please feel free to email me if you have any questions. <br/><br/>Ben<br/><br/>";

                        if ($participant_email == '') {
                            echo'<br/><br/><hr/><li><b><span style="color:red;">Participant Email Error Contact Ben</span></b></li>';
                            $Email_reader->move(1, 'Error Email');
                        } else {
                            if (mail($to, $subject, $msg, $headers)) {
                                $Email_reader->move(1);

                                echo '<br/><br/><hr/><li><span style="color:green;"><b>Following Email successfully sent to ' . $to . ':</b>';
                                echo '<br/><br/>' . $msg_to_print . '</span></li>';
                                $participant_email_to_store = $participant_email;
                                $donor_name_to_store = str_replace("\r\n", "", $donor_name[1]);
                                $donor_email_to_store = str_replace("\r\n", "", $donor_email[1]);
                                $donor_amount_to_store = str_replace("\r\n", "", $donor_amount[1]);
                                $donor_comment_to_store = 'None'; 

                                $array_for_storage = array(
                                    array(
                                        "participant_email" => $participant_email_to_store,
                                        "participant_password" => $participant_password,
                                        "participant_name" => $participant_name,
                                        "donor_name" => $donor_name_to_store,
                                        "donor_email" => $donor_email_to_store,
                                        "amount" => $donor_amount_to_store,
                                        "comment" => $donor_comment_to_store,
                                ));

                                $file = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/marathon/marathon-2024/marathon.json');
                                $data = json_decode($file);
                                unset($file); //prevent memory leaks for large json.
//insert data here
                                $data[] = $array_for_storage;
//save the file         
                                $json = json_encode($data);
                                //$json = str_replace("\r\n", "", $json);
                                file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/marathon/marathon-2024/marathon.json', $json);
                                //echo $json;

                                unset($data); //release memory
                            } else {
                                $Email_reader->move(1, 'Error Send');
                                echo'<br/><br/><hr/><li><b><span style="color:red;">Email Send Error Contact Ben</span></b></li>';
                            }
                        }
                    }
                }

This code is sending 2 emails and 2 outputs of the sent emails even with the above test data.

0

There are 0 best solutions below