This library simply gets the differences between strings with a few options to output the changes into raw array
, xml
or text
formats.
The recommended way to install is through composer.
composer require osiset/php-diff
<?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>