forked from chrisboulton/php-diff
-
Notifications
You must be signed in to change notification settings - Fork 4
1. Getting Started
Gitea edited this page Oct 29, 2021
·
2 revisions
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.
- Download the latest release from Github.
- Extract the contents of the downloaded file.
- Copy or move the contents of folder
php-diff-x.x.x
to a folder which is accessible by your project. - 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';
<?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>' ;