PHPackages                             philiprehberger/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. philiprehberger/php-diff

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

philiprehberger/php-diff
========================

Diff strings, arrays, and objects with unified, HTML, and structured output

v1.2.0(4mo ago)168MITPHPPHP ^8.2CI passing

Since Mar 15Pushed 1mo agoCompare

[ Source](https://github.com/philiprehberger/php-diff)[ Packagist](https://packagist.org/packages/philiprehberger/php-diff)[ Docs](https://github.com/philiprehberger/php-diff)[ GitHub Sponsors](https://github.com/philiprehberger)[ RSS](/packages/philiprehberger-php-diff/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (6)Versions (8)Used By (0)

PHP Diff
========

[](#php-diff)

[![Tests](https://github.com/philiprehberger/php-diff/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/php-diff/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/892eb7325f6164e3de65073eda2b1703bc2761b4b88539dd18f17d2a5d38ed1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f7068702d646966662e737667)](https://packagist.org/packages/philiprehberger/php-diff)[![Last updated](https://camo.githubusercontent.com/9138b64c058fbbbfc27b73839d872127a74af17374eb4418b8bb91612e27b7e8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f7068696c69707265686265726765722f7068702d64696666)](https://github.com/philiprehberger/php-diff/commits/main)

Diff strings, arrays, and objects with unified, HTML, and structured output.

Requirements
------------

[](#requirements)

- PHP 8.2+

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

[](#installation)

```
composer require philiprehberger/php-diff
```

Usage
-----

[](#usage)

### Comparing Strings

[](#comparing-strings)

```
use PhilipRehberger\Diff\Diff;

$diff = Diff::strings("hello\nworld", "hello\nphp");

$diff->hasChanges();         // true
$diff->toUnified();          // unified diff string
$diff->toHtml();             // HTML with / tags
$diff->toHtmlSideBySide();   // side-by-side HTML table
$diff->toAnsi();             // ANSI-colored terminal output
$diff->toArray();            // array of DiffLine objects
$diff->stats();              // DiffStats { added: 1, removed: 1, unchanged: 1 }
$diff->similarity();         // 0.333... (0.0 = completely different, 1.0 = identical)
```

### Ignore Options

[](#ignore-options)

```
use PhilipRehberger\Diff\Diff;

// Normalize whitespace before comparing
$diff = Diff::strings("hello  world", "hello world")->ignoreWhitespace();
$diff->hasChanges(); // false

// Case-insensitive comparison
$diff = Diff::strings("Hello", "hello")->ignoreCase();
$diff->hasChanges(); // false

// Ignore blank lines
$diff = Diff::strings("hello\n\nworld", "hello\nworld")->ignoreBlankLines();
$diff->hasChanges(); // false
```

### Context Lines

[](#context-lines)

```
use PhilipRehberger\Diff\Diff;

$diff = Diff::strings($oldText, $newText);

$diff->toUnified(1);   // 1 line of context around changes
$diff->toUnified(5);   // 5 lines of context around changes
```

### Similarity Score

[](#similarity-score)

```
use PhilipRehberger\Diff\Diff;

$diff = Diff::strings("hello\nworld", "hello\nphp");

$diff->similarity(); // 0.333... - one of three lines is unchanged
```

### Side-by-Side HTML

[](#side-by-side-html)

```
use PhilipRehberger\Diff\Diff;

$diff = Diff::strings("hello\nworld", "hello\nphp");

$html = $diff->toHtmlSideBySide();
//
//   hellohello
//   world
//   php
//
```

### Comparing Arrays

[](#comparing-arrays)

```
use PhilipRehberger\Diff\Diff;

$diff = Diff::arrays(
    ['name' => 'Alice', 'age' => 30, 'city' => 'NYC'],
    ['name' => 'Alice', 'age' => 31, 'country' => 'US'],
);

$diff->hasChanges();  // true
$diff->changes();     // all Change objects
$diff->added();       // entries only in the new array
$diff->removed();     // entries only in the old array
$diff->changed();     // entries with different values
```

### Comparing Objects

[](#comparing-objects)

```
use PhilipRehberger\Diff\Diff;

$old = (object) ['name' => 'Alice', 'age' => 30];
$new = (object) ['name' => 'Alice', 'age' => 31];

$diff = Diff::objects($old, $new);

$diff->hasChanges();  // true
$diff->changes();     // [PropertyChange { property: 'age', from: 30, to: 31 }]
```

API
---

[](#api)

### `Diff` (Static Entry Point)

[](#diff-static-entry-point)

MethodReturnsDescription`Diff::strings(string $old, string $new)``StringDiff`Compare two strings line by line`Diff::arrays(array $old, array $new)``ArrayDiff`Compare two arrays by key`Diff::objects(object $old, object $new)``ObjectDiff`Compare two objects by property### `StringDiff`

[](#stringdiff)

MethodReturnsDescription`ignoreWhitespace()``StringDiff`Normalize whitespace before comparing`ignoreCase()``StringDiff`Lowercase both inputs before comparing`ignoreBlankLines()``StringDiff`Remove blank lines before comparing`toUnified(int $context = 3)``string`Unified diff format`toAnsi(int $context = 3)``string`ANSI-colored unified diff for terminals`toHtml()``string`HTML with ins/del tags`toHtmlSideBySide()``string`Side-by-side HTML table`toArray()``array`Array of DiffLine value objects`hasChanges()``bool`Whether any differences exist`stats()``DiffStats`Count of added, removed, unchanged lines`similarity()``float`Ratio of unchanged to total lines (0.0-1.0)### `ArrayDiff`

[](#arraydiff)

MethodReturnsDescription`changes()``array`All changes`added()``array`Only added entries`removed()``array`Only removed entries`changed()``array`Only modified entries`hasChanges()``bool`Whether any differences exist### `ObjectDiff`

[](#objectdiff)

MethodReturnsDescription`changes()``array`All property changes`hasChanges()``bool`Whether any differences exist### Value Objects

[](#value-objects)

- **`DiffLine`** - `type` (`added`|`removed`|`unchanged`), `content`, `lineNumber`
- **`Change`** - `key`, `old`, `new`, `type` (`added`|`removed`|`changed`)
- **`PropertyChange`** - `property`, `from`, `to`
- **`DiffStats`** - `added`, `removed`, `unchanged`

Development
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
```

Support
-------

[](#support)

If you find this project useful:

⭐ [Star the repo](https://github.com/philiprehberger/php-diff)

🐛 [Report issues](https://github.com/philiprehberger/php-diff/issues?q=is%3Aissue+is%3Aopen+label%3Abug)

💡 [Suggest features](https://github.com/philiprehberger/php-diff/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)

❤️ [Sponsor development](https://github.com/sponsors/philiprehberger)

🌐 [All Open Source Projects](https://philiprehberger.com/open-source-packages)

💻 [GitHub Profile](https://github.com/philiprehberger)

🔗 [LinkedIn Profile](https://www.linkedin.com/in/philiprehberger)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance84

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.2% 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 ~2 days

Total

7

Last Release

133d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (15 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

phpdiffunified diffcomparemyershtml diff

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[jfcherng/php-diff

A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc).

4836.1M81](/packages/jfcherng-php-diff)[localheinz/diff

Fork of sebastian/diff for use with ergebnis/composer-normalize

4742.6M9](/packages/localheinz-diff)[jblond/php-diff

A comprehensive library for generating differences between two hashable objects (strings or arrays).

37131.9k1](/packages/jblond-php-diff)[jbzoo/composer-diff

See what has changed after a composer update.

55919.3k1](/packages/jbzoo-composer-diff)[fisharebest/algorithm

Implementation of standard algorithms in PHP.

72117.7k2](/packages/fisharebest-algorithm)[phalcongelist/php-diff

A comprehensive library for generating differences between two hashable objects (strings or arrays).

12435.7k2](/packages/phalcongelist-php-diff)

PHPackages © 2026

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