Comparing text: what is the best approach

198 Views Asked by At

I have a database which stores sequential versions of a text file. What I would like to do is allow the user to call up any two historical files (e.g version 1 And version 4) and compare them for differences:

  1. Spelling changes
  2. New words / new sentences / new paragraphs
  3. Moved paragraphs
  4. Etc

Basically an online GIT but for text files not code.

I figure JavaScript is probably the best approach and I presume that there must be an open source library but I simply cannot find one.

Any assistance appreciated.

1

There are 1 best solutions below

0
On

If you are running under linux, you could install diff, save both files in temp files, then compare them using the shell_exec command.

For example

$tmp1 = 'myfile.txt';
$tmp2 = 'myfile2.txt';    
$output = shell_exec('diff -y {$tmp1} {$tmp2}');

Look at man diff for an option that suits your needs.