ohmybrew/php-diff

This package is abandoned and no longer maintained. The author suggests using the https://github.com/osiset/php-diff package instead.

LSM - Longest Common Subsequence

v2.0.0 2020-03-02 05:07 UTC

This package is auto-updated.

Last update: 2020-03-02 05:10:15 UTC


README

This library simply gets the differences between strings with a few options to output the changes into raw array, xml or text formats.

Build Status Coverage Status License

Installation

The recommended way to install is through composer.

composer require osiset/php-diff

Usage

<?php

use Wally\PHPDiff\Diff;
use Wally\PHPDiff\Format\Text;

$diff = new Diff;
$diff->setStringOne(
<<<EOF
one
two
three
EOF
);
$diff->setStringTwo(
<<<EOF
one
two
threes
six
EOF
);
$diff->execute();

$result = $diff->getResult(); // This outputs a raw array of line, delete and insert operations.

$format = new Text($result);
$format->execute();

header('Content-Type: '.$format->getFormatMime());
print $format->getResult();

The output is:

one
two
- three
+ threes
+ six

Above was the use of the text output. You may also output the raw array via $diff->getResult() or you can ouput in XML format like below:

use Wally\PHPDiff\Format\XML;

...

$result = $diff->getResult();

$format = new XML($result);
$format->execute();

header('Content-Type: '.$format->getFormatMime());
print $format->getResult();

The output is:

<?xml version="1.0" encoding="UTF-8" ?>
<data>
<line>one</line>
<line>two</line>
<delete>three</delete>
<insert>threes</insert>
<insert>six</insert>
</data>