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:

samplename@sampledomain.com | samplename
samplename@sampledomain.com | 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..

janisch
  • 159
  • 1
  • 1
  • 13
  • 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. – M. Eriksson Oct 02 '19 at 15:49
  • You can read more about quotes in PHP [in this answer](https://stackoverflow.com/a/3446286/2453432). – M. Eriksson Oct 02 '19 at 15:54

1 Answers1

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;
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 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. – janisch Oct 02 '19 at 16:25
  • 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. – Nigel Ren Oct 02 '19 at 16:26
  • what do u mean with "add it to them when you display the data"? – janisch Oct 02 '19 at 16:31
  • edit: I just made a new line after my fwrite, which says: "fwrite($handle, "\n");" #closed – janisch Oct 03 '19 at 06:44