PHPackages                             yusitnikov/php-diff - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yusitnikov/php-diff

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yusitnikov/php-diff
===================

Text diff library for PHP

1.0.3(5mo ago)720.2k↓31%8MITPHPPHP ^7.1|^8.0

Since Apr 20Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/yusitnikov/php-diff)[ Packagist](https://packagist.org/packages/yusitnikov/php-diff)[ RSS](/packages/yusitnikov-php-diff/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

php-diff
========

[](#php-diff)

Text diff library for PHP

Installation
------------

[](#installation)

```
composer require yusitnikov/php-diff
```

Usage
-----

[](#usage)

Create an instance of `LevenshteinDiffCalculator`. Arguments:

1. `string $separatorRegex` - separator RegEx that will split the strings into units. By default, strings will be split into letters.
2. `OperationCostCalculator $operationCostCalculator` - specify it to apply custom operation costs. By default, the cost for each operation (insert, delete, replace) will be 1.
3. `StringDiffCalculatorInterface $itemDiffCalculator` - diff calculator that would be applied when replacing a unit.

### Example 1. Simple use-case - calculate distance and diff letter by letter.

[](#example-1-simple-use-case---calculate-distance-and-diff-letter-by-letter)

```
use Chameleon\PhpDiff\LevenshteinDiffCalculator;

$s1 = 'Levenshtein';
$s2 = 'Einstein';

$differ = new LevenshteinDiffCalculator();
var_dump($differ->calcDistance($s1, $s2));
var_dump($differ->calcDiff($s1, $s2));
```

Output:

```
int(5)
class Chameleon\PhpDiff\StringDiffResult#14 (2) {
  public $distance =>
  int(5)
  public $diff =>
  array(5) {
    [0] =>
    class Chameleon\PhpDiff\StringDiffOperation#12 (2) {
      public $operation =>
      string(6) "DELETE"
      public $content =>
      string(4) "Leve"
    }
    [1] =>
    class Chameleon\PhpDiff\StringDiffOperation#13 (2) {
      public $operation =>
      string(6) "INSERT"
      public $content =>
      string(2) "Ei"
    }
    [2] =>
    class Chameleon\PhpDiff\StringDiffOperation#7 (2) {
      public $operation =>
      string(5) "MATCH"
      public $content =>
      string(2) "ns"
    }
    [3] =>
    class Chameleon\PhpDiff\StringDiffOperation#5 (2) {
      public $operation =>
      string(6) "DELETE"
      public $content =>
      string(1) "h"
    }
    [4] =>
    class Chameleon\PhpDiff\StringDiffOperation#4 (2) {
      public $operation =>
      string(5) "MATCH"
      public $content =>
      string(4) "tein"
    }
  }
}

```

The corresponding diff:

```
- Leve
+ Ei
ns
- h
tein
```

### Example 2. Complex differ - compare lines and words

[](#example-2-complex-differ---compare-lines-and-words)

```
use Chameleon\PhpDiff\LevenshteinDiffCalculator;
use Chameleon\PhpDiff\OperationCostCalculator;

$s1 = 'And now here is my secret,
a very simple secret:
it is only with the heart that one can see rightly,
what is essential is invisible to the eye.

"The Little Prince", Antoine de Saint-Exupéry';

$s2 = '> So here is my secret,
> its only with heart that somebody can see rightly,
> what is essential is always invisible to the eye.
> I can promise it to you.

- Le Petit Prince, Antoine Marie Jean-Baptiste Roger, comte de Saint-Exupéry';
$lineDiffer = new LevenshteinDiffCalculator(LevenshteinDiffCalculator::SPLIT_WORDS_REGEX);
$textDiffer = new LevenshteinDiffCalculator(
    LevenshteinDiffCalculator::SPLIT_LINES_REGEX,
    (new OperationCostCalculator())->setReplaceDistanceCalculator($lineDiffer),
    $lineDiffer
);
$result = $textDiffer->calcDiff($s1, $s2);
```

[![Diff](README-screenshot.png)](README-screenshot.png)

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance70

Regular maintenance activity

Popularity35

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 64.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~804 days

Total

4

Last Release

171d ago

PHP version history (3 changes)v1.0.0PHP ^7.0

1.0.1PHP ^7.0|^8.0

1.0.3PHP ^7.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/58d76ba9d11931a4350c64c6b878ccf7b28f40c3a98ac7cc651d9fdc80a3162a?d=identicon)[yusitnikov](/maintainers/yusitnikov)

---

Top Contributors

[![yusitnikov](https://avatars.githubusercontent.com/u/13732463?v=4)](https://github.com/yusitnikov "yusitnikov (9 commits)")[![brian-florian](https://avatars.githubusercontent.com/u/130850276?v=4)](https://github.com/brian-florian "brian-florian (2 commits)")[![ysitnikov](https://avatars.githubusercontent.com/u/13730673?v=4)](https://github.com/ysitnikov "ysitnikov (2 commits)")[![AndrewFeeney](https://avatars.githubusercontent.com/u/13425337?v=4)](https://github.com/AndrewFeeney "AndrewFeeney (1 commits)")

### Embed Badge

![Health badge](/badges/yusitnikov-php-diff/health.svg)

```
[![Health](https://phpackages.com/badges/yusitnikov-php-diff/health.svg)](https://phpackages.com/packages/yusitnikov-php-diff)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
