Compare two text files, output line matches

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Engineerd
Forum Newbie
Posts: 7
Joined: Thu Dec 16, 2010 5:55 pm

Compare two text files, output line matches

Post by Engineerd »

I know this sounds simple, but I can't seem to properly compare lines from, say, two csv files and write the matches to another file.

Any help?

Love you guys btw.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Compare two text files, output line matches

Post by McInfo »

If you're looking for identical lines rather than changes that may have been made, there is a good chance this will work.

Code: Select all

// Puts matching lines from files A and B into C
file_put_contents('c', implode(array_intersect(file('a'), file('b'))));
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Compare two text files, output line matches

Post by Christopher »

If you are on a Unix box you could exec a program like diff to do this.
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Compare two text files, output line matches

Post by pickle »

Christopher wrote:If you are on a Unix box you could exec a program like diff to do this.
+1
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Engineerd
Forum Newbie
Posts: 7
Joined: Thu Dec 16, 2010 5:55 pm

Re: Compare two text files, output line matches

Post by Engineerd »

I am on a linux box/server.

Please let me know how I could best open file a, file b and write matchces to file c.

Thank you.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Compare two text files, output line matches

Post by McInfo »

Given "old" file A and "new" file B, the following command returns old lines as empty strings, new lines as empty strings, and unchanged lines as themselves. In other words, it returns only lines that exist in both A and B.* The string returned by diff through the standard output (STDOUT) is sent to file C.

Code: Select all

diff --old-line-format= --new-line-format= --unchanged-line-format=%L A B > C
* Note that, to be considered "unchanged", lines in file B must occur in the same order as they did in A. If file A has lines {a, b, c} and file B has lines {a, c, b}, file C will be given lines {a, c}. Line {b} is out of order, so it is considered "old" in file A and "new" in file B.

To execute the command from PHP, see Program Execution Functions.

For more information about diff, enter "man diff" at the command line or into your favorite search engine.
User avatar
Engineerd
Forum Newbie
Posts: 7
Joined: Thu Dec 16, 2010 5:55 pm

Re: Compare two text files, output line matches

Post by Engineerd »

Here's how I did it kiddos.

Main file:

Code: Select all


<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>OUTPUT MATCHES IN ANY TEXT, CSV, EXCEL DOCS</strong></td>
</tr>
<tr>
<td>Select file # 1
  <input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file # 2
  <input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
  <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<p>&nbsp;</p>

multiple_upload_ac.php

Code: Select all

<?php

$file1 = 'a.txt';
$file2 = 'b.txt';

//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];

//rename files to a.txt and b.txt

$path1= "upload/a.txt";
$path2= "upload/b.txt";
//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];


if($filesize1 && $filesize2 != 0)
{	
	// Perform comparison and export functions
	$a=file($path1);
	$b=file($path2);
	file_put_contents("matching_entries.txt", implode(array_intersect($a, $b)));
	echo "<a href=\"matching_entries.txt\">Click Here To See Matching Entries</a>";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

?>
User avatar
Engineerd
Forum Newbie
Posts: 7
Joined: Thu Dec 16, 2010 5:55 pm

Re: Compare two text files, output line matches

Post by Engineerd »

Just a note, the reason I did this is to make file manipulation that more flexible down-the-road.

Thanks all.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Compare two text files, output line matches

Post by McInfo »

$HTTP_POST_FILES is deprecated in favor of $_FILES.
User avatar
Engineerd
Forum Newbie
Posts: 7
Joined: Thu Dec 16, 2010 5:55 pm

Re: Compare two text files, output line matches

Post by Engineerd »

Can you show me the best way to replace?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Compare two text files, output line matches

Post by McInfo »

I just mean that using $_FILES is preferred.
User avatar
Engineerd
Forum Newbie
Posts: 7
Joined: Thu Dec 16, 2010 5:55 pm

Re: Compare two text files, output line matches

Post by Engineerd »

Note taken and will do moving forward!

Best
Post Reply