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
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
// Installed via composer...
require 'vendor/autoload.php';
// ...or installed manually.
require dirname(__FILE__).'/../lib/Autoloader.php';
new \jblond\Autoloader();
$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,
];
// Initialize the diff class.
$diff = new \jblond\Diff($a, $b /*, $options */);
// Choose Renderer.
$renderer = new \jblond\Diff\Renderer\Html\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.
- PHP 7.2 or greater
- PHP Multibyte String
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