This project provides a composer package with a differ for creating a unified diff of two strings.
Run
composer require ergebnis/differCreate a MyersDiffer, then call diff() with a From and a To, each made of a Label and a Text, to receive a Diff of the lines of both Texts:
<?php
declare(strict_types=1);
use Ergebnis\Differ;
$differ = new Differ\MyersDiffer();
$diff = $differ->diff(
Differ\From::create(
Differ\Label::fromString('original'),
Differ\Text::fromString("foo\nbar\nbaz\n"),
),
Differ\To::create(
Differ\Label::fromString('changed'),
Differ\Text::fromString("foo\nqux\nbaz\n"),
),
);
echo $diff->toString();This prints
--- original
+++ changed
@@ -1,3 +1,3 @@
foo
-bar
+qux
bazWhen both Texts are identical, Diff::isEmpty() returns true and Diff::toString() returns an empty string.
MyersDiffer implements the Differ interface, which you can use as a type declaration.
Label::fromString() throws an InvalidLabel exception when the value contains a line break, because a line break would break the header of the diff.
When both Texts end with a new line, Diff::toString() returns the same unified diff as sebastian/diff with its StrictUnifiedDiffOutputBuilder.
The maintainers of this project record notable changes to this project in a changelog.
The maintainers of this project suggest following the contribution guide.
The maintainers of this project ask contributors to follow the code of conduct.
The maintainers of this project provide limited support.
This project currently supports the following PHP versions:
- PHP 7.4 (has reached its end of life on November 28, 2022)
- PHP 8.0 (has reached its end of life on November 26, 2023)
- PHP 8.1 (has reached its end of life on December 31, 2025)
- PHP 8.2
- PHP 8.3
- PHP 8.4
- PHP 8.5
- PHP 8.6
The maintainers of this project add support for a PHP version following its initial release and may drop support for a PHP version when it has reached its end of life.
This project has a security policy.
This project uses the MIT license.
The algorithm for computing the diff in MyersDiffer has been adopted from SebastianBergmann\Diff\MyersDiff and SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder (originally licensed under BSD-3-Clause by Sebastian Bergmann), which implement the linear-space variant of the algorithm described by Eugene W. Myers in "An O(ND) Difference Algorithm and Its Variations".
Follow @localheinz and @ergebnis on X.