A comprehensive library for generating differences between two hashable objects (strings or arrays). Generated differences can be rendered in all of the standard formats including:
- Unified
- Context
- Inline HTML
- Side by Side HTML
- Unified HTML
- Unified Commandline colored output
The logic behind the core of the diff engine (ie, the sequence matcher) is primarily based on the Python difflib package. The reason for doing so is primarily because of its high degree of accuracy.
composer require jblond/php-diff
<?php
use jblond\Diff;
use jblond\Diff\Renderer\Html\SideBySide;
// Installed via composer...
require 'vendor/autoload.php';
$a = file_get_contents(dirname(__FILE__).'/a.txt');
$b = file_get_contents(dirname(__FILE__).'/b.txt');
// Options for generating the diff.
$options = [
'ignoreWhitespace' => true,
'ignoreCase' => true,
'context' => 2,
'cliColor' => 'simple' // for cli output
];
// Initialize the diff class.
$diff = new Diff($a, $b /*, $options */);
// Choose Renderer.
$renderer = new SideBySide([
'title1' => 'Custom title for OLD version',
'title2' => 'Custom title for NEW version',
]);
// Show it.
echo $diff->Render($renderer);
A quick usage example can be found in the example/
directory and under example.php. Included is a light theme and a dark theme.
More Example Pictures
- PHP 7.2 or greater
- PHP Multibyte String
- jblond/php-cli
Xiphe has build a jQuery plugin with that you can merge the compared files. Have a look at jQuery-Merge-for-php-diff.
- Ability to ignore blank line changes
- 3 way diff support
Contributors since I forked the repo.
- maxxer
- Creris
- jfcherng
- DigiLive
see License
composer run-script phpunit
composer run-script php_src
composer run-script php_test