Skip to content
Gitea edited this page Oct 29, 2021 · 2 revisions

Getting Started

Installation

Composer

PHP-Diff is available on Packagist (using semantic versioning), and installation via Composer is the recommended way for installation. Just add the following line to the required section of your composer.json file:

"jblond/php-diff": "~2.0"

or run the following command in your cli:

composer require jblond/php-diff

Note that the vendor folder and the vendor/autoload.php script are generated by Composer; They are not part of PHP- Diff.

Manual

  1. Download the latest release from Github.
  2. Extract the contents of the downloaded file.
  3. Copy or move the contents of folder php-diff-x.x.x to a folder which is accessible by your project.
  4. Load each class file manually.

E.g.

use jblond\Diff;
use jblond\Diff\Renderer\Html\SideBySide;

require 'path/to/php-diff/lib/jblond/Diff.php';
require 'path/to/php-diff/lib/jblond/Diff/Renderer/HTML/SideBySide.php';

Example

<?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,
];

// 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>' ;
Clone this wiki locally