Jump to content

php Compare two text files and output NON matching records


increase

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>';

 

Link to comment
Share on other sites

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);
}}error.thumb.png.020988ab07382bcee5916ab99c7660f1.png
 
?>

Edited by increase
Link to comment
Share on other sites

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 by increase
Link to comment
Share on other sites

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>";

 

Link to comment
Share on other sites

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.