0

I'm back with another question and I hope you can help me.

I have a text file with e-mail adresses and names like this:

[email protected] | samplename
[email protected] | samplename

Now I want to transform this text file into a array, so I can proof with "file_get_contents", if an email or name is already in this list or not.

How do I do this?

I already tried many pieces of code, but nothing worked.

Relevant code:

 $email = $_GET['email'].PHP_EOL;
 $name = $_GET['name'].PHP_EOL;
 $file = file_get_contents('anfragen.txt', true);
 if (in_array("$name",'$file') || in_array("$email", '$file'))

Error: (caused by the last line of code)

in_array() expects parameter 2 to be array, string given in C:\xampp\htdocs\tutorial2.php on line 9

Why is my code not working? What do I have to change? Pls help me..

2
  • 1
    if you do: '$file' (with single quotes), you're creating a string with the literal content $file. That means that you're passing in a string as the second argument. If you want to use the variable, there's no reason to quote them at all (double quotes will work, but are unnecessary): in_array($name, $file). However, that will only return true if the complete file content matches and not on partial matches. Commented Oct 2, 2019 at 15:49
  • You can read more about quotes in PHP in this answer. Commented Oct 2, 2019 at 15:54

1 Answer 1

1

Using file_get_contents() returns a string with the contents of the file, so you would have to process this into an array to use in_array(). It will also read in all of the file every time and possibly building arrays of every entry even if the first row matches.

There are a few issues with the code, so I'll post a version which should do as you want. This treats the file as a CSV, although delimited by | (also have to trim the fields to compare them properly).

I've added comments to show what I've done...

$email = $_GET['email'];    // Don't add PHP_EOL
$name = $_GET['name'];      // Don't add PHP_EOL
// Open file for reading
$file = fopen('anfragen.txt', "r");
$found = false;
// Read line as a csv, with | as the delimeter
while ( $line = fgetcsv($file, null, "|"))    {
    // Check row for matching details
    if ( trim($line[0]) == $email || trim($line[1]) == $name )   {
        echo "found".PHP_EOL;
        // Flag that row is found and stop reading
        $found = true;
        break;
    }
}
fclose($file);
if ( !$found )  {
    echo "Not found".PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, thx! That worked! But I have one last question: Was it essential to take out "PHP_EOL"? Because that made a new line after every mail and name, and that was pretty useful.
If you want to display them with new lines add it to them when you display the data, but when you want to do the comparison, you would have to add it to the email off the file as well to get a match.
what do u mean with "add it to them when you display the data"?
edit: I just made a new line after my fwrite, which says: "fwrite($handle, "\n");" #closed

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.