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

ActiveLibrary

sitefinitysteve/php-diff-text
=============================

PHP library to generate HTML diff output with multiple diff strategies. PHP variant of vue-diff-text.

1.5.4(1mo ago)00MITPHPPHP &gt;=8.1CI passing

Since Mar 10Pushed 1mo agoCompare

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

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

php-diff-text
=============

[](#php-diff-text)

PHP library to generate HTML diff output with multiple diff strategies. Zero external dependencies.

> This is a PHP variant of [vue-diff-text](https://github.com/sitefinitysteve/vue-diff-text), a Vue 3 plugin for displaying text and HTML differences. Same diff strategies, same HTML output, same CSS classes — just in PHP.

**Author:** [Steve McNiven-Scott](https://www.sitefinitysteve.com)

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

[](#installation)

```
composer require sitefinitysteve/php-diff-text
```

That's it — Composer's autoloader handles the rest. No service providers, no config files.

Usage
-----

[](#usage)

Each diff class takes old and new text and returns HTML with `.diff-added` and `.diff-removed` spans.

### Quick start (any PHP project)

[](#quick-start-any-php-project)

```
use PhpDiffText\DiffText;

// One-liner — pick your strategy
echo DiffText::words('The quick brown fox', 'The slow brown fox');
echo DiffText::chars('cat', 'car');
echo DiffText::lines($oldFile, $newFile);
```

Or use the individual classes directly:

```
use PhpDiffText\DiffWords;

echo DiffWords::render('The quick brown fox', 'The slow brown fox');

// Get raw Change[] array for custom rendering
$changes = DiffWords::diff('old text', 'new text');
```

### Laravel Blade example

[](#laravel-blade-example)

In your controller:

```
use PhpDiffText\DiffText;

public function show()
{
    return view('document.diff', [
        'diffHtml' => DiffText::words($oldVersion, $newVersion),
    ]);
}
```

In your Blade template:

```
{{-- Include the diff styles (in your layout or the specific view) --}}

{{-- Render the diff output (already safe HTML) --}}
{!! $diffHtml !!}
```

> **Tip:** Copy `vendor/sitefinitysteve/php-diff-text/css/style.css` into `public/vendor/php-diff-text/style.css`, or add it to your Vite/Mix pipeline.

Output:

```

  The
  quick
  slow
  brown
  fox

```

### Available Diff Classes

[](#available-diff-classes)

ClassDescription`DiffChars`Character-level diff`DiffWords`Word-level diff (ignores whitespace)`DiffWordsWithSpace`Word-level diff (whitespace-aware)`DiffLines`Line-level diff`DiffSentences`Sentence-level diff`DiffHtml`HTML-aware diff with optional similarity thresholdAll methods are static — no instantiation needed.

### DiffText Facade

[](#difftext-facade)

The `DiffText` class is the recommended entry point:

```
use PhpDiffText\DiffText;

DiffText::chars($old, $new);
DiffText::words($old, $new);
DiffText::wordsWithSpace($old, $new);
DiffText::lines($old, $new);
DiffText::sentences($old, $new);
DiffText::html($old, $new, similarityThreshold: 0.3);
```

### Options

[](#options)

All text diff classes accept an options array:

```
DiffWords::render('Hello World', 'hello world', ['ignoreCase' => true]);
```

### HTML Diff with Similarity Threshold

[](#html-diff-with-similarity-threshold)

`DiffHtml` supports a similarity threshold (0-1). When the texts are less similar than the threshold, it renders a "full replacement" instead of a granular diff:

```
use PhpDiffText\DiffText;

// Word-level diff (default)
echo DiffText::html('Hello world', 'Hello Vue world');

// Full replacement when texts are very different
echo DiffText::html(
    'Original long paragraph about insurance policies...',
    'Item 1: House. Item 2: Car.',
    similarityThreshold: 0.3
);
```

### Similarity Utility

[](#similarity-utility)

Compute text similarity directly:

```
use PhpDiffText\Similarity;

$score = Similarity::compute('Hello world', 'Hello worlds');
// Returns float 0-1 (Dice coefficient)
```

### Styling

[](#styling)

Include the bundled CSS for default diff styling:

```

```

Or copy it into your asset pipeline. Customize with CSS variables:

```
:root {
  --text-diff-added-bg: #ddfbe6;
  --text-diff-added-color: #008000;
  --text-diff-removed-bg: #fce9e9;
  --text-diff-removed-color: #c70000;
  --text-diff-removed-decoration: line-through;
}
```

Testing
-------

[](#testing)

```
composer install
composer test
```

Publishing to Packagist
-----------------------

[](#publishing-to-packagist)

### First-time setup

[](#first-time-setup)

1. Create a GitHub repository:

    ```
    cd php-diff-text
    git init
    git add .
    git commit -m "Initial release"
    gh repo create sitefinitysteve/php-diff-text --public --source=. --push
    ```
2. Register on [Packagist](https://packagist.org):

    - Log in with your GitHub account
    - Click "Submit" and enter the GitHub repo URL
    - Packagist will auto-detect the `composer.json`
3. Set up auto-updating (recommended):

    - On Packagist, go to your package settings and grab the API token
    - On GitHub, go to repo Settings &gt; Webhooks &gt; Add webhook
    - Payload URL: `https://packagist.org/api/github?username=sitefinitysteve`
    - Content type: `application/json`
    - Secret: your Packagist API token
    - Events: "Just the push event"

### Releasing a new version

[](#releasing-a-new-version)

Tag a release and push:

```
git tag v1.0.0
git push origin v1.0.0
```

Or use GitHub Releases:

```
gh release create v1.0.0 --title "v1.0.0" --notes "Initial release"
```

Packagist picks up the new tag automatically via the webhook.

### GitHub Actions (optional auto-test on release)

[](#github-actions-optional-auto-test-on-release)

Create `.github/workflows/tests.yml`:

```
name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ['8.1', '8.2', '8.3', '8.4']
    steps:
      - uses: actions/checkout@v4
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
      - run: composer install --no-interaction
      - run: composer test
```

License
-------

[](#license)

MIT

---

Made with ❤️ by [sitefinitysteve](https://www.sitefinitysteve.com)

###  Health Score

37

—

LowBetter than 82% of packages

Maintenance94

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

59d ago

### Community

Maintainers

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

---

Top Contributors

[![sitefinitysteve](https://avatars.githubusercontent.com/u/1542376?v=4)](https://github.com/sitefinitysteve "sitefinitysteve (3 commits)")

---

Tags

diffcomparisonhtml difftext-diffphp-diff

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[sebastian/diff

Diff implementation

7.7k901.5M249](/packages/sebastian-diff)[jfcherng/php-diff

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

4705.1M51](/packages/jfcherng-php-diff)[caxy/php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

21520.9M14](/packages/caxy-php-htmldiff)[localheinz/diff

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

4637.0M5](/packages/localheinz-diff)[diff/diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

2041.6M12](/packages/diff-diff)[ion-bazan/composer-diff

Compares composer.lock changes and generates Markdown report so you can use it in PR description.

1861.8M3](/packages/ion-bazan-composer-diff)

PHPackages © 2026

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