(Linux)
In the following program I'm trying to process the file accounts.txt line by line searching for a match to the form supplied variable '$email'. The function fgets() is stepping though the file line by line correctly but my attempts at using preg_match() and or strpos() to determine if that line contains the string contained in '$email' have failed. Any ideas or suggestions? Thanks.

<?php

$email = $_POST['email'];
$psswd = $_POST['psswd'];

echo "The email address is $email<br>";
echo "The password is $psswd<br><br>";

$file = fopen("./accounts.txt", 'r+') or die("Failed to open file");

while(!feof($file))
{
    $line = fgets($file);
    if(preg_match($email, $line))
    {
        echo "There was a match";
    }
    else
    {
        echo "There was not match";
    }

    //echo "$line<br>";
}
//$line = fgets(preg_match($email, '$file'));


fclose($file);

?>

Recommended Answers

All 3 Replies

Code for reading file:

<?php
$file = fopen("members.txt", "r");
$members = array();

while (!feof($file)) {
   $members[] = fgets($file);
}

fclose($file);

var_dump($members);
?>

The function returns the following values:

int strcmp ($string1, $string2);

Returns < 0 if string1 is less than string2.
Returns > 0 if string1 is greater than string2.
Returns 0 if they are equal.

My program is stepping through the file line by line correctly, what I can't seem to be able to do is determine if the string supplied by the '$email' variable is a substring in the current line being processed.

I also got this error doing it that way.

[error] [client 127.0.0.1] PHP Notice: Array to string conversion in /var/www/ET/password/accounts.php on line 16

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.