A comprehensive library for generating differences between two hashable objects (strings or arrays). Generated differences can be rendered in all the standard formats including:
- Unified
- Context
- Side by Side HTML
- Unified HTML
- Unified Commandline colored output
- JSON
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
For cli usage you need to install the suggested jblond/php-cli
package.
See the Wiki for
<?php
use jblond\Diff;
use jblond\Diff\Renderer\Html\SideBySide;
// Installed via composer...
require 'vendor/autoload.php';
$sampleA = file_get_contents(dirname(__FILE__).'/a.txt');
$sampleB = file_get_contents(dirname(__FILE__).'/b.txt');
// Options for generating the diff.
$options = [
'ignoreWhitespace' => true,
'ignoreCase' => true,
'context' => 2,
'cliColor' => true, // for cli output
'ignoreLines' => Diff::DIFF_IGNORE_LINE_BLANK,
];
// Initialize the diff class.
$diff = new Diff($sampleA, $sampleB /*, $options */);
// Choose Renderer.
$renderer = new SideBySide([
'title1' => 'Custom title for sample A',
'title2' => 'Custom title for sample B',
]);
// Show the output of the difference renderer.
echo $diff->Render($renderer);
// Alternative
// Show the differences or a message.
echo $diff->isIdentical() ? 'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>' ;
File example.php
contains a quick demo and can be found in the example/
directory. Included is a light and a dark theme.
More Example Pictures
- PHP 7.3 or greater
- PHP Multibyte String
- jblond/php-cli (suggested)
If you found a bug, or have an idea for new functionality, feel free to report it on the issue tracker - just use search beforehand. Issue tracker
You can also fork this repository and open a PR.
Xiphe has build a jQuery plugin with that you can merge the compared files. Have a look at jQuery-Merge-for-php-diff .
- 3 way diff support
Contributors since I forked the repo.
see License
composer run-script phpunit
composer run-script php_src
composer run-script php_test