Saladon
Saladon

Reputation: 85

Looking for duplicate in local text file

please could someone help me with this duplicate checking function. I'm still quite fresh when it comes to PHP, so please excuse me, if this is a simple fix.

I'm storing a list of email addresses to a file called list.txt. Here is the content of the file (each on a new line):

[email protected]
[email protected]
[email protected]

Now I have a function (not working) that should check if email is already in the list:

function is_unique($email) {
   $list = file('list.txt');

   foreach($list as $item){
      if($email == $item){
          return false;
          die(); 
      }
   }
   return true;
}

When I call the function in this test, with an existing email address, it still returns true:

if( is_unique('[email protected]') ) {
    echo "Email is unique";
} else {
    echo "Duplicate email";
}

// Returns true even though [email protected] is in the list

I appreciate anyone's input.

Upvotes: 6

Views: 171

Answers (6)

Samuel Cook
Samuel Cook

Reputation: 16828

It is fine to use list but you must trim() the item when iterating through the list as they still have the linebreak:

if($email == trim($item))

Update: As Nick mentioned, the optional flag of FILE_IGNORE_NEW_LINES would dramatically cut down on execution speed and code:

function is_unique($email) {
   $list = file('list.txt',FILE_IGNORE_NEW_LINES);
   return in_array($email,$list) ? false : true;  
}

Upvotes: 7

Ejaz
Ejaz

Reputation: 8872

You want to look out for carriage return and line feed at end of each line of file

$fp = fopen('emails.txt', 'r');
while (!feof($fp))
{
   $eml = fgets($fp);
   if(trim($eml, " \n\r") == $email)
   {
      return false;
   }
}
return true;

Upvotes: 0

Nick
Nick

Reputation: 6346

Either remember to trim your email, or a better solution would be to use FILE_IGNORE_NEW_LINES while you use the file function, which would allow you to use PHP's inbuilt array searching (should be faster). For example:

function is_unique($email) {
   $list = file('list.txt',FILE_IGNORE_NEW_LINES);

   return !in_array($email,$list);
}

Upvotes: 2

salik
salik

Reputation: 13

for list all non-duplicate emails:

$list=array_unique(file('list.txt'));

for check if duplicates exists:

$list1=file('list.txt');
$list2=array_unique(file('list.txt'));
count($list1)==count($list2);

Upvotes: 0

Luigi Siri
Luigi Siri

Reputation: 2018

You can read the file line by line and compare the content of that line with your new email.

Something like this:

function is_unique($email) 
{
   $file = fopen("list.txt", "r") or exit("Unable to open file!");

   while(!feof($file))
   {
      if($email == $file){
      return false;
      die();
   }
   fclose($file);
}

Hope this helps.

Upvotes: 0

Anyone
Anyone

Reputation: 2835

What you can do is read the file -using fopen- and read every line separately. Then you just simply check if the name is already in there. You can also open it, put the entire file in an array (every line = array row) and throw over an array_unique to filter out doubles.

Upvotes: 0

Related Questions