Skip to content
A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc). Based on the difflib implementation in Python
Branch: master
Clone or download
Pull request Compare This branch is 183 commits ahead, 5 commits behind chrisboulton:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
lib
tests/Diff/Renderer
.editorconfig
.gitignore
LICENSE
README.md
composer.json
phpcs.xml
readme.png
readme2.png

README.md

PHP Diff Class

SensioLabsInsight Codacy Badge

Latest Version

Introduction

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

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.

Install

composer require jblond/php-diff

Example Use

<?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);

Example Output

A quick usage example can be found in the example/ directory and under example.php.

Example Image

Example 2 Image

Requirements

  • 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.

Todo

  • Ability to ignore blank line changes
  • 3 way diff support

Contributors

Contributors since I forked the repo.

  • maxxer
  • Creris
  • jfcherng

License (BSD License)

see License

tests

composer run-script phpunit
composer run-script php_src
composer run-script php_test
You can’t perform that action at this time.