PHPackages                             jsandersuk/string-diffs - 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. jsandersuk/string-diffs

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

jsandersuk/string-diffs
=======================

Locates and highlights differences between two strings

0.0.1(7y ago)033MITPHPPHP &gt;=7.1.0

Since Jun 1Pushed 7y agoCompare

[ Source](https://github.com/jsandersuk/string-diffs)[ Packagist](https://packagist.org/packages/jsandersuk/string-diffs)[ RSS](/packages/jsandersuk-string-diffs/feed)WikiDiscussions master Synced 3d ago

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

String difference calculator
============================

[](#string-difference-calculator)

This package compares two strings and highlights any text that has been removed or inserted.

Basic usage
-----------

[](#basic-usage)

To generate a "diff" between two strings you need to create an instance of the Calculator and provide it with a stylist - a class that deals with styling the removed and inserted text in order to highlight it to the end-user.

The example below is designed to output diffs to a console/terminal. See [Styling output](#styling-output) for information on how to change the output style.

```
$stylist = new JSandersUK\StringDiffs\Stylists\ConsoleStylist();
$calculator = new JSandersUK\StringDiffs\Calculator($stylist);

$styledDifferenceText = $calculator->diff($old, $new);
print $styledDifferenceText;
```

Styling output
--------------

[](#styling-output)

The removed and inserted text in a diff is styled before being returned in order to highlight the differences to the end-user.

You will need to choose the relevant "stylist" class to match the environment where you will be displaying the diff.

### Outputting to a console/terminal

[](#outputting-to-a-consoleterminal)

When printing a diff to a console you can use the *ConsoleStylist* which uses ANSI escape sequences to colour the removed and inserted text.

By default removed text is output as white on red and inserted text white on green. Colours can be configured by passing custom values to the *constructor* of the class.

#### Basic usage

[](#basic-usage-1)

```
$stylist = new JSandersUK\StringDiffs\Stylists\ConsoleStylist();
```

#### Usage with custom colours

[](#usage-with-custom-colours)

The package makes use of the *bramus/ansi-php* package to colour the text and expects its constants to set colours.

**NOTE** the difference between foreground and background constants - COLOR\_FG\_WHITE vs COLOR\_BG\_WHITE; "FG" (foreground) and "BG" (background)

```
$stylist = new JSandersUK\StringDiffs\Stylists\ConsoleStylist(
    Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR::COLOR_FG_WHITE,
    Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR::COLOR_BG_RED,
    Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR::COLOR_FG_WHITE,
    Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR::COLOR_BG_GREEN
);
```

### Outputting to HTML

[](#outputting-to-html)

When printing a diff to a browesr you can use the *HtmlStylist* which wraps removed an inserted text in HTML elements with class applied.

By default text is wrapped in a SPAN and removed text has a class of *removed-text* applied and inserted text *inserted-text*. The HTML element to wrap text and the two classes can be configured by passing custom values to the *constructor* of the class.

#### Basic usage

[](#basic-usage-2)

```
$stylist = new JSandersUK\StringDiffs\Stylists\HtmlStylist();;
```

#### Usage with HTML classes and element

[](#usage-with-html-classes-and-element)

```
$stylist = new JSandersUK\StringDiffs\Stylists\HtmlStylist(
    'old-text',
    'new-text',
    'div'
);
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

2905d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b09bb0d934ff8376333a843fcd66ab8c91b5558227adc7956be35607c2fd7ee?d=identicon)[james.sanders](/maintainers/james.sanders)

### Embed Badge

![Health badge](/badges/jsandersuk-string-diffs/health.svg)

```
[![Health](https://phpackages.com/badges/jsandersuk-string-diffs/health.svg)](https://phpackages.com/packages/jsandersuk-string-diffs)
```

PHPackages © 2026

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