Highlight diffs provided by Sebastian Bergmann diff
composer.phar require sokil/php-diff
<?php
use Sokil\Diff\Change;
use Sokil\Diff\Renderer;
$diffRenderer = new Renderer();
$actualDiff = $diffRenderer->render(new Change(
implode(PHP_EOL, ['line1', 'line2', 'line3']),
implode(PHP_EOL, ['line1', 'line2changed', 'line3'])
));
Format of diff tags may be configured. By default renders only two tags: <ins>
and <del>
. To
highlight output, use predefined format:
<?php
$renderer = new Renderer([
'format' => Renderer::FORMAT_COLOUR
]);
This will produce following HTML:
line1
<del style="background: #ffe7e7;">line2</del>
<ins style="background: #ddfade;">line2changed</ins>
line3
To fully customize style, use next syntax (this format has alias Renderer::FORMAT_COLOUR
):
<?php
$renderer = new Renderer([
'format' => [
'insert' => [
'tag' => 'ins',
'attributes' => 'style="background: #ddfade;"',
],
'delete' => [
'tag' => 'del',
'attributes' => 'style="background: #ffe7e7;"',
]
]
]);
// this is same to
$renderer = new Renderer([
'format' => Renderer::FORMAT_COLOUR
]);
Default format Renderer::FORMAT_DEFAULT
has following notation:
<?php
$renderer = new Renderer([
'insert' => [
'tag' => 'ins',
],
'delete' => [
'tag' => 'del',
]
]);
// this is same to
$renderer = new Renderer([
'format' => Renderer::FORMAT_DEFAULT
]);