PHP Diff Class
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:
- Inline HTML
- Side by Side 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 manual require dirname(__FILE__).'/../lib/Autoloader.php'; new \jblond\Autoloader(); $a = explode("\n", file_get_contents(dirname(__FILE__).'/a.txt')); $b = explode("\n", file_get_contents(dirname(__FILE__).'/b.txt')); // Options for generating the diff $options = array( //'ignoreWhitespace' => true, //'ignoreCase' => true, ); // Initialize the diff class $diff = new \jblond\Diff($a, $b, $options); //choose renderer $renderer = new \jblond\Diff\Renderer\Html\SideBySide(array( 'title_a' => 'Custom title for OLD version', 'title_b' => '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.1 or greater
- PHP Multibyte String
Merge files using jQuery
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.
License (BSD License)
composer run-script phpunit composer run-script php_src composer run-script php_test