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

License

Notifications You must be signed in to change notification settings

JBlond/php-diff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ec897dd · Jul 13, 2020
Jul 13, 2020
Jul 13, 2020
Mar 11, 2020
Jan 19, 2019
Jan 29, 2020
May 14, 2020
Jul 13, 2020
Jul 8, 2020
Jun 6, 2020
Jun 6, 2020
Jul 3, 2020
Jun 6, 2020
Oct 8, 2019
Jun 6, 2020
Jun 6, 2020
Jul 3, 2020

Repository files navigation

PHP Diff Class

SensioLabsInsight Codacy Badge

Latest Version Packagist Installs

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

Install

composer require jblond/php-diff

Example Use

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

Example Output

A quick usage example can be found in the example/ directory and under example.php. Included is a light theme and a dark theme.

HTML Side By Side Example

HTML Side By Side Example

More Example Pictures

HTML Inline Example

HTML Inline Example

HTML Unified Example

HTML Unified Example

Text Unified Example

Text Unified Example

Text Context Example

Text Context Example

Text Unified Console Example

Text Unified Console Example

HTML Side By Side Dark Theme Example

HTML Side By Side Dark Theme Example

Requirements

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

License (BSD License)

see License

Tests

composer run-script phpunit
composer run-script php_src
composer run-script php_test

About

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

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • PHP 95.4%
  • CSS 4.6%