0
votes

How can I check and find differences(e.g svn diff, netbeans diff) between two files using php?

Platform : Win 7 32bit, PHP 5.4.7 (VC9 X86 32bit thread safe), XAMPP Version 1.8.1

Loaded extensions : Core, bcmath, calendar, ctype, date, ereg, filter, ftp, hash, iconv, json, mcrypt, SPL, odbc, pcre, Reflection, session, standard, mysqlnd, tokenizer, zip, zlib, libxml, dom, PDO, bz2, SimpleXML, wddx, xml, xmlreader, xmlwriter, apache2handler, Phar, curl, mbstring, exif, gd, gettext, mysql, mysqli, pdo_mysql, pdo_sqlite, soap, sockets, sqlite3, xmlrpc, xsl, mhash

2
use, file() function, compare lines, highlight different parts. P.S.: Have you tried anything?BlitZ
@CORRUPT I didn't find any script I used meld found from this answer stackoverflow.com/questions/11895570/… I could have created by my self but for now I need quick solutionARIF MAHMUD RANA

2 Answers

2
votes

This all depends on how you wish to compare the files. Are they text files or binary files etc. If we assume they are text files you have to decide if white spaces should be ignored etc. What you could do is open the two files to compare and go through them character by character and compare them. What it boils down to is to solve the Longest Common Sub-sequence problem. I would recommend you to start looking at algorithms for comparing like found here for example

http://en.wikipedia.org/wiki/Diff#Algorithm

Another approach would be to include patch (which can be found for Windows to) and run that in the background to find differences and build patch files (since my guess is that you want to find the differences and be able to store them and maybe apply them later?)

2
votes

XDiff extension is useful to your case,

xdiff extension enables you to create and apply patch files containing differences between different revisions of files.

This extension supports two modes of operation - on strings and on files, as well as two different patch formats - unified and binary. Unified patches are excellent for text files as they are human-readable and easy to review. For binary files like archives or images, binary patches will be adequate choice as they are binary safe and handle non-printable characters well.

Reference:

http://php.net/manual/en/function.xdiff-file-diff.php