jblond/php-diff

这是一个用于生成两个可哈希对象(字符串或数组)之间差异的综合库。

v2.4.0 2021-10-26 18:11 UTC

README

Codacy Badge Latest Version Packagist Installs

简介

这是一个用于生成两个可哈希对象(字符串或数组)之间差异的综合库。生成的差异可以以所有标准格式呈现,包括

  • 统一格式
  • 上下文格式
  • 并排HTML
  • 统一HTML
  • 统一命令行彩色输出
  • JSON

差异引擎核心(即序列匹配器)的逻辑主要基于Python的difflib包。这样做的主要原因是因为其高精度。

安装

composer require jblond/php-diff

安装以支持命令行

对于命令行使用,您需要安装建议的jblond/php-cli包。

文档

有关入门参数和选项自定义渲染器样式变更日志示例用法示例输出,请参阅Wiki。

变更日志

变更日志

示例用法

<?php
use jblond\Diff;
use jblond\Diff\Renderer\Html\SideBySide;

// Installed via composer...
require 'vendor/autoload.php';

$sampleA = file_get_contents(dirname(__FILE__).'/a.txt');
$sampleB = file_get_contents(dirname(__FILE__).'/b.txt');

// Options for generating the diff.
$options = [
    'ignoreWhitespace' => true,
    'ignoreCase'       => true,
    'context'          => 2,
    'cliColor'         => true, // for cli output
    'ignoreLines'      => Diff::DIFF_IGNORE_LINE_BLANK,
];

// Initialize the diff class.
$diff = new Diff($sampleA, $sampleB /*, $options */);

// Choose Renderer.
$renderer = new SideBySide([
    'title1' => 'Custom title for sample A',
    'title2' => 'Custom title for sample B',
]);

// Show the output of the difference renderer.
echo $diff->Render($renderer);

// Alternative
// Show the differences or a message.
echo $diff->isIdentical() ? 'No differences found.' : '<pre>' . htmlspecialchars($diff->render($renderer)) . '</pre>' ;

示例输出

文件example.php包含一个快速演示,可在example/目录中找到。包括浅色和深色主题。

HTML并排示例

HTML Side By Side Example

更多示例图片

HTML统一示例

HTML Unified Example

文本统一示例

Text Unified Example

文本上下文示例

Text Context Example

文本统一控制台示例

Text Unified Console Example

HTML并排深色主题示例

HTML Side By Side Dark Theme Example

需求

  • PHP 7.3或更高版本
  • PHP多字节字符串
  • jblond/php-cli(建议)

贡献、问题和功能请求

如果您发现了一个错误,或者有关于新功能的想法,请随时在问题跟踪器中报告它——请先进行搜索。问题跟踪器

您也可以fork此存储库并打开一个PR。

使用jQuery合并文件

Xiphe已经构建了一个jQuery插件,您可以使用它来合并比较的文件。请参阅jQuery-Merge-for-php-diff

待办事项

  • 三路差异支持

贡献者

自从我fork了存储库以来,的贡献者。

许可证(BSD许可证)

请参阅许可证

测试

composer run-script phpunit
composer run-script php_src
composer run-script php_test