2017 © Pedro Peláez
 

library string-diffs

Locates and highlights differences between two strings

image

jsandersuk/string-diffs

Locates and highlights differences between two strings

  • Friday, June 1, 2018
  • by james.sanders
  • Repository
  • 0 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

String difference calculator

This package compares two strings and highlights any text that has been removed or inserted., (*1)

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., (*2)

The example below is designed to output diffs to a console/terminal. See Styling output for information on how to change the output style., (*3)

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

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

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., (*4)

You will need to choose the relevant "stylist" class to match the environment where you will be displaying the diff., (*5)

Outputting to a console/terminal

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

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., (*7)

Basic usage

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

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., (*8)

NOTE the difference between foreground and background constants - COLOR_FG_WHITE vs COLOR_BG_WHITE; "FG" (foreground) and "BG" (background), (*9)

$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

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

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., (*11)

Basic usage

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

Usage with HTML classes and element

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

The Versions

01/06 2018

dev-master

9999999-dev

Locates and highlights differences between two strings

  Sources   Download

MIT

The Requires

 

by James Sanders

01/06 2018

0.0.1

0.0.1.0

Locates and highlights differences between two strings

  Sources   Download

MIT

The Requires

 

by James Sanders