Stars: 190
Forks: 63
Pull Requests: 69
Issues: 53
Watchers: 43
Last Updated: 2023-08-14 13:49:30
A library for comparing two HTML files/snippets and highlighting the differences using simple HTML. Includes support for comparing complex lists and tables
License: GNU General Public License v2.0
Languages: PHP
php-htmldiff is a library for comparing two HTML files/snippets and highlighting the differences using simple HTML.
This HTML Diff implementation was forked from rashid2538/php-htmldiff and has been modified with new features, bug fixes, and enhancements to the original code.
For more information on these modifications, read the differences from rashid2538/php-htmldiff or view the CHANGELOG.
https://php-htmldiff.caxy.com/
The recommended way to install php-htmldiff is through Composer. Require the caxy/php-htmldiff package by running following command:
composer require caxy/php-htmldiff
This will resolve the latest stable version.
Otherwise, install the library and setup the autoloader yourself.
If you are using Symfony, you can use the caxy/HtmlDiffBundle to make life easy!
use Caxy\HtmlDiff\HtmlDiff;
$htmlDiff = new HtmlDiff($oldHtml, $newHtml);
$content = $htmlDiff->build();
See https://github.com/caxy/php-htmldiff/blob/master/demo/codes.css for starter CSS you can use for displaying the HTML diff output.
The configuration for HtmlDiff is contained in the Caxy\HtmlDiff\HtmlDiffConfig
class.
There are two ways to set the configuration:
When a new HtmlDiff
object is created, it creates a HtmlDiffConfig
object with the default configuration.
You can change the configuration using setters on the object:
use Caxy\HtmlDiff\HtmlDiff;
// ...
$htmlDiff = new HtmlDiff($oldHtml, $newHtml);
// Set some of the configuration options.
$htmlDiff->getConfig()
->setMatchThreshold(80)
->setInsertSpaceInReplace(true)
;
// Calculate the differences using the configuration and get the html diff.
$content = $htmlDiff->build();
// ...
You can also set the configuration by creating an instance of
Caxy\HtmlDiff\HtmlDiffConfig
and using it when creating a new HtmlDiff
object using HtmlDiff::create
.
This is useful when creating more than one instance of HtmlDiff
:
use Caxy\HtmlDiff\HtmlDiff;
use Caxy\HtmlDiff\HtmlDiffConfig;
// ...
$config = new HtmlDiffConfig();
$config
->setMatchThreshold(95)
->setInsertSpaceInReplace(true)
;
// Create an HtmlDiff object with the custom configuration.
$firstHtmlDiff = HtmlDiff::create($oldHtml, $newHtml, $config);
$firstContent = $firstHtmlDiff->build();
$secondHtmlDiff = HtmlDiff::create($oldHtml2, $newHtml2, $config);
$secondHtmlDiff->getConfig()->setMatchThreshold(50);
$secondContent = $secondHtmlDiff->build();
// ...
$config = new HtmlDiffConfig();
$config
// Percentage required for list items to be considered a match.
->setMatchThreshold(80)
// Set the encoding of the text to be diffed.
->setEncoding('UTF-8')
// If true, a space will be added between the <del> and <ins> tags of text that was replaced.
->setInsertSpaceInReplace(false)
// Option to disable the new Table Diffing feature and treat tables as regular text.
->setUseTableDiffing(true)
// Pass an instance of \Doctrine\Common\Cache\Cache to cache the calculated diffs.
->setCacheProvider(null)
// Disable the HTML purifier (only do this if you known what you're doing)
// This bundle heavily relies on the purified input from ezyang/htmlpurifier
->setPurifierEnabled(true)
// Set the cache directory that HTMLPurifier should use.
->setPurifierCacheLocation(null)
// Group consecutive deletions and insertions instead of showing a deletion and insertion for each word individually.
->setGroupDiffs(true)
// List of characters to consider part of a single word when in the middle of text.
->setSpecialCaseChars(array('.', ',', '(', ')', '\''))
// List of tags (and their replacement strings) to be diffed in isolation.
->setIsolatedDiffTags(array(
'ol' => '[[REPLACE_ORDERED_LIST]]',
'ul' => '[[REPLACE_UNORDERED_LIST]]',
'sub' => '[[REPLACE_SUB_SCRIPT]]',
'sup' => '[[REPLACE_SUPER_SCRIPT]]',
'dl' => '[[REPLACE_DEFINITION_LIST]]',
'table' => '[[REPLACE_TABLE]]',
'strong' => '[[REPLACE_STRONG]]',
'b' => '[[REPLACE_B]]',
'em' => '[[REPLACE_EM]]',
'i' => '[[REPLACE_I]]',
'a' => '[[REPLACE_A]]',
))
// Sets whether newline characters are kept or removed when `$htmlDiff->build()` is called.
// For example, if your content includes <pre> tags, you might want to set this to true.
->setKeepNewLines(false)
;
See CONTRIBUTING file.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See CODE_OF_CONDUCT file.
Did we miss anyone? If we did, let us know or put in a pull request!
php-htmldiff is available under GNU General Public License, version 2. See the LICENSE file for details.