PHP code example of baraja-core / simple-php-diff

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

$diff = (new SimpleDiff)->compare($left, $right);

echo 'Changed lines: ' . implode(', ', $diff->getChangedLines());

$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;
}

$diff = (new SimpleDiff)->compare($left, $right);

$lines = explode("\n", $diff->getDiff());
$html = '<div class="my-diff-container">';

foreach ($lines as $line) {
    $firstChar = $line[0] ?? '';
    $cssClass = match ($firstChar) {
        '+' => 'diff-added',
        '-' => 'diff-removed',
        default => 'diff-unchanged',
    };
    $html .= sprintf('<div class="%s">%s</div>', $cssClass, htmlspecialchars($line));
}

$html .= '</div>';
echo $html;

function showCommitDiff(string $oldContent, string $newContent): string
{
    $simpleDiff = new SimpleDiff;
    $diff = $simpleDiff->compare($oldContent, $newContent);

    $changedCount = count($diff->getChangedLines());

    $output = "<h3>Changes: {$changedCount} line(s) modified</h3>";
    $output .= $simpleDiff->renderDiff($diff);

    return $output;
}