0

I've got a piece of code searching a text file for an email address, returning the two numbers on the same line. The file looks like:

24/08/2017,[email protected],1,2
21/05/2018,[email protected],1,2
21/05/2018,[email protected],2,2

My code currently looks like

$lines = file("log.txt");
$found = 0;


foreach ($lines as $line){      
    if (strpos($line, $email) !==false){
        $found = true;

        $arrayOfLine = explode(",", $line);

        $foundGroup = $arrayOfLine[2]; 
        $foundVideo = $arrayOfLine[3]; 

    }
    elseif (strpos($line, $email) ===false){
        $found = false;
    }
}

When I run this code, through a HTML form that takes in the email address to be searched for, it only finds matching emails if they were the last entered - in my example above, [email protected] wouldn't return a match, but [email protected] would. What am I missing that's preventing it from turning up matches?

2
  • Have no idea what you are doing with the $found var, but: 3v4l.org/GittA May 22, 2018 at 14:39
  • @Mike This is just a section of the larger code, the overall purpose is to create the list but I can't have duplicates, so this section searches for matches, and would show that the email exists in the file already should it find a match.
    – Lou C
    May 22, 2018 at 14:40

2 Answers 2

1

You are not terminating your loop when you find an answer, meaning it continues to run even if it found a match and overwrites any previous match. Add a break; after you assigned $foundGroup and $foundVideo.

You also have two evaluations checking the same thing. Set the $found flag to false at the start of the loop. If your loop fails to find a match, it'll still be false. You don't need to compare twice.

$found = false;

foreach ($lines as $line){      
    if (strpos($line, $email) !==false){
        $found = true;

        $arrayOfLine = explode(",", $line);

        $foundGroup = $arrayOfLine[2]; 
        $foundVideo = $arrayOfLine[3]; 
        break; // stop searching for more matches
    }
}
0

This is actually pretty easy to figure out.

it only finds matching emails if they were the last entered

This is true. Why? Because you always set $found to false, unless the email address you're searching for is the last one. Even if you have found a match, you're just continuing the loop and overwriting $found with false.

Take a look at this snippet and see if you can figure it out.

You either need to break out of the loop, or stop setting $found this way.

foreach ($lines as $line){      
    if (strpos($line, $email) !==false) {
        $found = true;

        $arrayOfLine = explode(",", $line);

        $foundGroup = $arrayOfLine[2]; 
        $foundVideo = $arrayOfLine[3]; 

    }
    elseif (strpos($line, $email) ===false) {
        $found = false; // This will be run every loop!
    }
}
4
  • This code will continue to search for email addresses even after a match is found, which is inefficient.
    – Mike
    May 22, 2018 at 14:45
  • Which is exactly what I described in my answer.
    – Loek
    May 22, 2018 at 14:47
  • Not sure what you're getting at. Both of your examples have the same error. Either way if your answer is to have the OP "figure it out," then I suppose you could put that for every answer on stackoverflow.
    – Mike
    May 22, 2018 at 14:48
  • Meh maybe. OP's question was What am I missing that's preventing it from turning up matches?, which is what I explained. Didn't give a working piece of code though, that is true.
    – Loek
    May 22, 2018 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.