Jump to content

Recommended Posts

Hello,

 

I have two flat text files, 1) users.txt and 2) class.txt

 

What I want to do is find all the people that ARE in the users.txt file BUT ARE NOT in the class.txt file

 

Here are the file formats:

 

1) users.txt

 

Email:FirstName:LastName:UserName:Password:Status

doe@college.net:John:Doe:jdoe:free1:0

 

*If the status, is a 0 then the user is a student, if it is a 1 then the user is a teacher.

So I will also only want to return the people that are students from the users.txt file.

 

2) class.txt

Email:FirstName:LastName

doe@college.net:John:Doe

 

So basically I would be doing the search by email of course...

 

I want to find all the students who are in the users.txt file, but NOT in the class file.

 

Then I want to return their first name and last name on a page to be selected to be added to the class file.

 

Any help would be great!

 

Thanks,

jcjst21

Link to comment
https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/
Share on other sites

This would be a lot easier with an actual database system such as MySQL since all you'll have to do is a few queries. If it's something you will need to do a lot it would probably be simplest to simple convert the files you have into tables in a database. If it's a one off type thing you will just have to use one of the file functions to read the file. You could use file to read the lines into an array explode the lines on the colon and parse it from there. You could use fgetcsv to read each line as an array one at a time. There are also other ways, that's just the first two that came to mind. Once you have the files in your script as an array doing the actual comparisons should be a trivial task.

I agree with cags: This would definitely be easier with a database.

 

I think something like this would work for what you are trying to do, though:

<?php
$f = fopen("users.txt", "r");
$users = array();
while (!feof($f))
   $users[] = explode(":", fgets($f));
fclose($f);

$f = fopen("class.txt", "r");
$class = array();
while (!feof($f))
   $class[] = explode(":", fgets($f));
fclose($f);

$addUsers = array();
foreach ($class as $c)
{
   $found = false;
   foreach ($users as $u)
   {
      if ($u[0] == $c[0])
      {
         $found = true;
         break;
      }
   }
   if (!$found)
      $addUsers[] = $c;  
}
?>

 

Then, $addUsers would contain all the information for that user so you could print it.

After you get the corresponding information from the two files into two arrays - http://us.php.net/manual/en/function.array-diff.php

If you have a multi-dimensional array and that suggestion isn't possible (since passing a multidimensional array and comparing it to another which has a different amount of items in the sub arrays won't work) you should be able to use array_udiff and write a simple function to compare $a[0] with $b[0].

I was getting really bored so I wrote this :P hope it works

 

$regexp = "/[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}/";
preg_match_all($regexp, file_get_contents('users.txt'), $user_matches);
$user_matches = $user_matches[0];
preg_match_all($regexp, file_get_contents('class.txt'), $class_matches);
$class_matches = $class_matches[0];
print_r(array_diff($user_matches, array_intersect($user_matches, $class_matches)));

You could also make use of the SPL, in particular the SplFileObject to get at the colon-separated-data in an object-oriented way.  Here's an example which should (in theory) be pretty straightforward to understand.

 

$users   = new SplFileObject('users.txt');
$classes = new SplFileObject('class.txt');

foreach (array($users, $classes) as $file) {
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(':');
$file->headings = $file->current();
}

// Get a list of students
foreach ($users as $user) {
$user = array_combine($users->headings, $user);
// Collect only students
if ($user['Status'] === '0') {
	$students[$user['Email']] = $user;
}
}

// Iterate over classes
foreach ($classes as $class) {
$class = array_combine($classes->headings, $class);
// Remove from students list
if (isset($students[$class['Email']])) {
	unset($students[$class['Email']]);
}
}

print_r($students);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.