Overview
Main php block Demo result Full code
php diff
Written: Jan-2024

php-diff - general differencing class

link: https://github.com/chrisboulton/php-diff/tree/master

This is a demonstrator for the chrisboulton php-diff class found on github, and is my preference for displaying differences between large code blocks using fairly simple calls

Main php block

demo.php :: main php block
require_once dirname(__FILE__).'/lib/Diff.php';
require_once dirname(__FILE__).'/lib/Diff/Renderer/Html/SideBySide.php';

$renderer = new Diff_Renderer_Html_SideBySide;
$options = array(
    'ignoreWhitespace' => true,
    'ignoreCase' => true,
);

$titleA = "/demo-data/a.php";
$titleB = "/demo-data/b.php";

$a=explode(PHP_EOL,file_get_contents(dirname(__FILE__).$titleA));
$b=explode(PHP_EOL,file_get_contents(dirname(__FILE__).$titleB));

$start_time = microtime(true);

$diff = new Diff($a, $b, $options);
$result=$diff->Render($renderer);

$end_time = microtime(true);
$execution_time = ($end_time - $start_time) * 1000; // Convert to milliseconds

$result.="<li>Operation took approximately " . number_format($execution_time, 2) . " milliseconds</li>";

// Change the table column titles....
$result=preg_replace("/Old Version/", $titleA, $result, 1); // 1 = only first occurrence to be changed
$result=preg_replace("/New Version/", $titleB, $result, 1);

Demo result

The demo here is using files a.php and b.php based on the source code of lib/Diff.php, with a few comments added.

The code results:

Full code

This is the full code being used in the demo if you're interested:

demo.php :: full php code
<?php
//--Code:start -all

//error_reporting(E_ALL);
//ini_set('display_errors', true);

//--MainCode:start
require_once dirname(__FILE__).'/lib/Diff.php';
require_once dirname(__FILE__).'/lib/Diff/Renderer/Html/SideBySide.php';

$renderer = new Diff_Renderer_Html_SideBySide;
$options = array(
    'ignoreWhitespace' => true,
    'ignoreCase' => true,
);

$titleA = "/demo-data/a.php";
$titleB = "/demo-data/b.php";

$a=explode(PHP_EOL,file_get_contents(dirname(__FILE__).$titleA));
$b=explode(PHP_EOL,file_get_contents(dirname(__FILE__).$titleB));

$start_time = microtime(true);

$diff = new Diff($a, $b, $options);
$result=$diff->Render($renderer);

$end_time = microtime(true);
$execution_time = ($end_time - $start_time) * 1000; // Convert to milliseconds

$result.="<li>Operation took approximately " . number_format($execution_time, 2) . " milliseconds</li>";

// Change the table column titles....
$result=preg_replace("/Old Version/", $titleA, $result, 1); // 1 = only first occurrence to be changed
$result=preg_replace("/New Version/", $titleB, $result, 1);

//--MainCode:end
echo <<<_EOD
<html>
<head>
<style>
.Differences {
    font-family:Courier New;
    font-size:12px;
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    empty-cells: show;
}

.Differences thead th {
    text-align: left;
    border-bottom: 1px solid #000;
    background: #eceaea;
    color: #000;
    padding: 4px;
}
.Differences tbody th {
    text-align: right;
    background: #eceaea;
    width: 4em;
    padding: 1px 2px;
    border-right: 1px solid #000;
    vertical-align: top;
    font-size: 13px;
}

.Differences td {
    padding: 1px 2px;
    font-family: Courier New, Consolas, monospace;
    font-size: 13px;
}

.DifferencesSideBySide .ChangeInsert td.Left {
    background: #dfd;
}

.DifferencesSideBySide .ChangeInsert td.Right {
    background: #cfc;
}

.DifferencesSideBySide .ChangeDelete td.Left {
    background: #f88;
}

.DifferencesSideBySide .ChangeDelete td.Right {
    background: #faa;
}

.DifferencesSideBySide .ChangeReplace .Left {
    background: #fe9;
}

.DifferencesSideBySide .ChangeReplace .Right {
    background: #fd8;
}

.Differences ins, .Differences del {
    text-decoration: none;
}

.DifferencesSideBySide .ChangeReplace ins, .DifferencesSideBySide .ChangeReplace del {
    background: #fc0;
}

.Differences .Skipped {
    background: #f7f7f7;
}

.DifferencesInline .ChangeReplace .Left,
.DifferencesInline .ChangeDelete .Left {
    background: #fdd;
}

.DifferencesInline .ChangeReplace .Right,
.DifferencesInline .ChangeInsert .Right {
    background: #dfd;
}

.DifferencesInline .ChangeReplace ins {
    background: #9e9;
}

.DifferencesInline .ChangeReplace del {
    background: #e99;
}

pre {
    width: 100%;
    overflow: auto;
}
</style>
</head>
<body>
$result
</body>
</html>
_EOD


link Click to view a demo

square