GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - JBlond/php-diff: 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

BranchesTags
Open more actions menu

Latest commit

 

History

595 Commits
595 Commits

Folders and files

NameName
Last commit message
Last commit date
assets
assets
 
 
example
example
 
 
src
src
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
changelog.md
changelog.md
 
 
composer.json
composer.json
 
 
docker-compose.yml
docker-compose.yml
 
 
generateChangelog.php
generateChangelog.php
 
 
phpcs.xml
phpcs.xml
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

PHP Diff Class

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

Install

composer require jblond/php-diff

Install for cli support

For cli usage you need to install the suggested jblond/php-cli package.

Documentation

See the Wiki for

  • Getting started
  • Parameters and Options
  • Custom Renderer
  • Styling

Changelog

Changelog

Example Use

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

Example Output

File example.php contains a quick demo and can be found in the example/ directory. Included is a light and a dark theme.

HTML Side By Side Example

HTML Side By Side Example

More Example Pictures

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

  • PHP 7.3 or greater
  • PHP Multibyte String
  • jblond/php-cli (suggested)

Contribution, Issues and feature requests

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.

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

  • 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

contextdiffphppsr-12psr-4side-by-sidediffsuccessorunified

Resources

Readme
BSD-3-Clause license
Activity

Stars

36 stars

Watchers

5 watching

Forks

4 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.