Back to the module list

Compute differences between two files or string

The xdiff class is a replacement of system diff command, but only written in PHP, and without dependancy.

It allow to generate Normal, Unified or SideBySide differences between two files or two strings.

Example 1 : Normal mode

$xdiff = new xdiff ();
$res = $xdiff->diff ("\n", "NEWLINE\n");
1c1
< 
---
> NEWLINE

Example 2 : Unified mode

$xdiff = new xdiff ("unified");
$res = $xdiff->diff ("\n", "NEWLINE\n");

--- Original 2019-03-15 14:12:25.000000000 +0100
+++ New 2019-03-15 14:12:25.000000000 +0100
@@ -1 +1 @@
-
+NEWLINE

Example 3 : Side by side mode

$xdiff = new xdiff ("sideBySide");
$res = $xdiff->diffFile ("/tmp/test_xdiff1", "/tmp/test_xdiff2");
Result :
                              > This is an important
                              > notice! It should
                              > therefore be located at
                              > the beginning of this
                              > document!
                              > 
This part of the                This part of the
document has stayed the         document has stayed the
same from version to            same from version to
version.  It shouldn't          version.  It shouldn't
be shown if it doesn't          be shown if it doesn't
change.  Otherwise, that        change.  Otherwise, that
would not be helping to         would not be helping to
compress the size of the        compress the size of the
changes.                        changes.

This paragraph contains       <
text that is outdated.        <
It will be deleted in the     <
near future.                  <
                              <
It is important to spell        It is important to spell
check this dokument. On       | check this document. On
the other hand, a               the other hand, a
misspelled word isn't           misspelled word isn't
the end of the world.           the end of the world.
Nothing in the rest of          Nothing in the rest of
this paragraph needs to         this paragraph needs to
be changed. Things can          be changed. Things can
be added after it.              be added after it.
                              >
                              > This paragraph contains
                              > important new additions
                              > to this document.

The class definition

Class \xdiff

Namespace \

Description

 Create a diff from two strings, array or files
 The output is compatible with "patch" command.

Properties

No property available

Methods

public function __construct ( $output="normal")
 The constructor allow to choose the output.
 @param string $output The output mode [Normal|Unified|SideBySide]

final public function computeArray ( $array1, $array2)
 Compute the differences between two arrays $array1 and $array2
 @param array $array1 The first array to compare
 @param array $array2 The second array to compare
 @return array The data in internal format

public function diff ( $string1, $string2)
 Compute the differences between two strings $string1 and $string2
 @param string $string1 The first string to compare
 @param string $string2 The second string to compare

public function diffArray ( $array1, $array2)
 Compute the differences between two arrays $array1 and $array2
 @param array $array1 The first array to compare
 @param array $array2 The second array to compare

public function diffFile ( $file1, $file2)
 Compute the differences between two files $file1 and $file2
 @param string $file1 The first file to use to compare
 @param string $file2 The second file to use to compare

public function setScreenWidth ( $screenWidth)
 Allow to set the side by side width to the maximum allowed by screenWidth
 @param integer $screenWidth The maximum width of the screen