increase Posted March 24, 2022 Share Posted March 24, 2022 I have two text files of data the first file has 30 lines of data and matches with 30 lines in the second text file, but in addition the first text file has two additional lines that are added as the operator uploads file to the directory I want to find the non matching lines and out put them to be used in the same script as a mailout. I am trying to use if (strcmp($textperline, $textperline1) !== 0) { echo 'Both strings are not equal'; echo $textperline1; // the line that is different echo "<br>"; } But it outputs the whole list of data, can anyone help listingout only NON matching lines? Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/ Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 You need to ensure that the files are in the same sequence or else you have to loop thru one file and compare each line to ALL of the lines in the other file. Plus you have to be sure that the files are all using the same case or else convert each file to a single case and compare them. Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594523 Share on other sites More sharing options...
Barand Posted March 24, 2022 Share Posted March 24, 2022 Perhaps... FILE 1 FILE 2 --------------------------------- --------------------------------- Twas brillig and the slithy toves Twas brillig and the slithy toves did gyre and gimble in the wabe. did gyre and gimble in the wabe. All mimsy were the borogoves All mimsy were the borogoves and the mome raths outgrabe. and the mome raths outgrabe. additional line 1. additional line 2. then $file1 = file('file1.txt', FILE_IGNORE_NEW_LINES); $file2 = file('file2.txt', FILE_IGNORE_NEW_LINES); echo '<pre>' . print_r(array_diff($file2, $file1), 1) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594524 Share on other sites More sharing options...
increase Posted March 24, 2022 Author Share Posted March 24, 2022 (edited) 18 minutes ago, Barand said: $file1 = file('file1.txt', FILE_IGNORE_NEW_LINES); $file2 = file('file2.txt', FILE_IGNORE_NEW_LINES); echo '<pre>' . print_r(array_diff($file2, $file1), 1) . '</pre>'; Thanks for your help, do I insert your suggested code into my script? Here is the code I am using <?php if ($file1 = fopen(".data1.txt", "r")) { while(!feof($file1)) { $textperline = fgets($file1); echo $textperline; echo "<br>";} if ($file2 = fopen(".data.txt", "r")) { while(!feof($file2)) {$textperline1 = fgets($file2); echo $textperline1; echo "<br>";} fclose($file1); fclose($file2); }} ?> Edited March 24, 2022 by increase Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594526 Share on other sites More sharing options...
Barand Posted March 24, 2022 Share Posted March 24, 2022 My code is the whole script to compare 2 text files and show the differences. Run it on its own but you will have to adjust for the names of your text files. Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594528 Share on other sites More sharing options...
increase Posted March 24, 2022 Author Share Posted March 24, 2022 (edited) 6 minutes ago, Barand said: My code is the whole script to compare 2 text files and show the differences. Run it on its own but you will have to adjust for the names of your text files. Thanks, I only get Array ( ) as the output, sorry i am real new to php, here is your code I inserted <?php $file1 = file('.data.txt', FILE_IGNORE_NEW_LINES); $file2 = file('.data1.txt', FILE_IGNORE_NEW_LINES); echo '<pre>' . print_r(array_diff($file2, $file1), 1) . '</pre>'; ?> Edited March 24, 2022 by increase Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594532 Share on other sites More sharing options...
ginerjm Posted March 24, 2022 Share Posted March 24, 2022 If you had tested whether or not the 'open' ran successfully you would have received an error message. Try taking the leading dot off your filenames. if (!$file1 = file('data.txt', FILE_IGNORE_NEW_LINES)) echo "Could not open file1<br>"; if (!$file2 = file('data1.txt', FILE_IGNORE_NEW_LINES)) echo "Could not open file2<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594534 Share on other sites More sharing options...
Barand Posted March 24, 2022 Share Posted March 24, 2022 Try swapping $file1 and $file2 echo '<pre>' . print_r(array_diff($file1, $file2), 1) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594535 Share on other sites More sharing options...
increase Posted March 24, 2022 Author Share Posted March 24, 2022 4 minutes ago, Barand said: Try swapping $file1 and $file2 echo '<pre>' . print_r(array_diff($file1, $file2), 1) . '</pre>'; wonderful I have my output thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/314622-php-compare-two-text-files-and-output-non-matching-records/#findComment-1594536 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.