1. Go to this page and download the library: Download baraja-core/simple-php-diff library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
baraja-core / simple-php-diff example snippets
use Baraja\DiffGenerator\SimpleDiff;
$left = 'First text';
$right = 'Second text';
$diff = (new SimpleDiff)->compare($left, $right);
// Output the diff as plain text
echo '<code><pre>' . htmlspecialchars((string) $diff) . '</pre></code>';
$simpleDiff = new SimpleDiff;
$diff = $simpleDiff->compare($left, $right);
// Returns styled HTML with color-coded changes
echo $simpleDiff->renderDiff($diff);
// Non-strict comparison (default)
$diff = (new SimpleDiff)->compare($left, $right);
$diff = (new SimpleDiff)->compare($left, $right, false);
// Strict comparison - preserves line endings
$diff = (new SimpleDiff)->compare($left, $right, true);
$diff = (new SimpleDiff)->compare($left, $right);
// Get the normalized original text
$original = $diff->getOriginal();
// Get the normalized target text
$target = $diff->getTarget();
$diff = (new SimpleDiff)->compare($left, $right);
// Using getter method
$diffString = $diff->getDiff();
// Using string casting (equivalent)
$diffString = (string) $diff;
$diff = (new SimpleDiff)->compare($left, $right);
$changedLines = $diff->getChangedLines();
// Example output: [2, 5, 8] - lines 2, 5, and 8 were modified
foreach ($changedLines as $lineNumber) {
echo "Line {$lineNumber} was changed\n";
}
// Check if any changes occurred
if (count($changedLines) === 0) {
echo "No differences found!";
}
$originalFile = file_get_contents('/path/to/original.txt');
$modifiedFile = file_get_contents('/path/to/modified.txt');
$simpleDiff = new SimpleDiff;
$diff = $simpleDiff->compare($originalFile, $modifiedFile);
// Check if files are identical
if (empty($diff->getChangedLines())) {
echo "Files are identical.";
} else {
echo "Files differ on lines: " . implode(', ', $diff->getChangedLines());
echo "\n\n";
echo $diff;
}