husslela03 Posted November 30, 2009 Share Posted November 30, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/ Share on other sites More sharing options...
abazoskib Posted November 30, 2009 Share Posted November 30, 2009 check out comm(http://www.oreillynet.com/linux/cmd/cmd.csp?path=c/comm) Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968398 Share on other sites More sharing options...
cags Posted November 30, 2009 Share Posted November 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968404 Share on other sites More sharing options...
husslela03 Posted November 30, 2009 Author Share Posted November 30, 2009 I know what I have to do for the arrays and taking the files in the arrays, but I suppose my biggest question is doing the for loops to check the arrays...and then writing them an array to display... Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968407 Share on other sites More sharing options...
PFMaBiSmAd Posted November 30, 2009 Share Posted November 30, 2009 After you get the corresponding information from the two files into two arrays - http://us.php.net/manual/en/function.array-diff.php Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968418 Share on other sites More sharing options...
lemmin Posted November 30, 2009 Share Posted November 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968419 Share on other sites More sharing options...
cags Posted November 30, 2009 Share Posted November 30, 2009 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]. Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968424 Share on other sites More sharing options...
rajivgonsalves Posted November 30, 2009 Share Posted November 30, 2009 I was getting really bored so I wrote this 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))); Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968435 Share on other sites More sharing options...
salathe Posted November 30, 2009 Share Posted November 30, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/183454-check-two-flat-text-files/#findComment-968455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.