superscanner / finediff
PHP implementation of a Fine granularity Diff engine
2.0.0
2022-06-14 10:05 UTC
Requires
- php: >=8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.7
- mockery/mockery: ^1.3
- phpstan/phpstan: ^1.4
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.1
- phpunit/phpunit: ^8.5 || ^9.2
- spaze/phpstan-disallowed-calls: ^2.2
This package is not auto-updated.
Last update: 2024-11-13 19:52:42 UTC
README
Originally written by Raymond Hill (https://github.com/gorhill/PHP-FineDiff)
Installation
composer require superscanner/finediff
Usage
Render HTML
Render the difference between two strings as HTML:
$diff = new FineDiff\Diff(); echo $diff->render('string one', 'string two');
This would then output:
string <ins>tw</ins>o<del>ne</del>
You could change the granularity to CogPowered\FineDiff\Granularity\Word
$diff = new FineDiff\Diff(new FineDiff\Granularity\Word()); // Or via the setter method: $diff->setGranularity(new FineDiff\Granularity\Word());
so the output is:
string <del>one</del><ins>two</ins>
You do this by passing it into the Diff constructor:
Grab opcode instructions
Opcode instructions are what tell FineDiff how to change one string into another.
$diff = new FineDiff\Diff(); echo $diff->getOpcodes('string one', 'string two');
This would then output:
c7d3i3:two
Render text using the opcodes:
$renderer = new FineDiff\Render\Text(); echo $renderer->process('string one', 'c7d3i3:two');
Would output:
string two
Same with HTML:
$renderer = new FineDiff\Render\Html(); echo $renderer->process('string one', 'c7d3i3:two');
Credits
Originally Sponsored by D4H. Sponsored by Superscanner.