philiprehberger/php-diff
Composer 安装命令:
composer require philiprehberger/php-diff
包简介
Diff strings, arrays, and objects with unified, HTML, and structured output
README 文档
README
Diff strings, arrays, and objects with unified, HTML, and structured output.
Requirements
- PHP 8.2+
Installation
composer require philiprehberger/php-diff
Usage
Comparing Strings
use PhilipRehberger\Diff\Diff; $diff = Diff::strings("hello\nworld", "hello\nphp"); $diff->hasChanges(); // true $diff->toUnified(); // unified diff string $diff->toHtml(); // HTML with <ins>/<del> tags $diff->toHtmlSideBySide(); // side-by-side HTML table $diff->toAnsi(); // ANSI-colored terminal output $diff->toArray(); // array of DiffLine objects $diff->stats(); // DiffStats { added: 1, removed: 1, unchanged: 1 } $diff->similarity(); // 0.333... (0.0 = completely different, 1.0 = identical)
Ignore Options
use PhilipRehberger\Diff\Diff; // Normalize whitespace before comparing $diff = Diff::strings("hello world", "hello world")->ignoreWhitespace(); $diff->hasChanges(); // false // Case-insensitive comparison $diff = Diff::strings("Hello", "hello")->ignoreCase(); $diff->hasChanges(); // false // Ignore blank lines $diff = Diff::strings("hello\n\nworld", "hello\nworld")->ignoreBlankLines(); $diff->hasChanges(); // false
Context Lines
use PhilipRehberger\Diff\Diff; $diff = Diff::strings($oldText, $newText); $diff->toUnified(1); // 1 line of context around changes $diff->toUnified(5); // 5 lines of context around changes
Similarity Score
use PhilipRehberger\Diff\Diff; $diff = Diff::strings("hello\nworld", "hello\nphp"); $diff->similarity(); // 0.333... - one of three lines is unchanged
Side-by-Side HTML
use PhilipRehberger\Diff\Diff; $diff = Diff::strings("hello\nworld", "hello\nphp"); $html = $diff->toHtmlSideBySide(); // <table class="diff-table"> // <tr><td class="diff-left diff-unchanged">hello</td><td class="diff-right diff-unchanged">hello</td></tr> // <tr><td class="diff-left diff-removed">world</td><td class="diff-right"></td></tr> // <tr><td class="diff-left"></td><td class="diff-right diff-added">php</td></tr> // </table>
Comparing Arrays
use PhilipRehberger\Diff\Diff; $diff = Diff::arrays( ['name' => 'Alice', 'age' => 30, 'city' => 'NYC'], ['name' => 'Alice', 'age' => 31, 'country' => 'US'], ); $diff->hasChanges(); // true $diff->changes(); // all Change objects $diff->added(); // entries only in the new array $diff->removed(); // entries only in the old array $diff->changed(); // entries with different values
Comparing Objects
use PhilipRehberger\Diff\Diff; $old = (object) ['name' => 'Alice', 'age' => 30]; $new = (object) ['name' => 'Alice', 'age' => 31]; $diff = Diff::objects($old, $new); $diff->hasChanges(); // true $diff->changes(); // [PropertyChange { property: 'age', from: 30, to: 31 }]
API
Diff (Static Entry Point)
| Method | Returns | Description |
|---|---|---|
Diff::strings(string $old, string $new) |
StringDiff |
Compare two strings line by line |
Diff::arrays(array $old, array $new) |
ArrayDiff |
Compare two arrays by key |
Diff::objects(object $old, object $new) |
ObjectDiff |
Compare two objects by property |
StringDiff
| Method | Returns | Description |
|---|---|---|
ignoreWhitespace() |
StringDiff |
Normalize whitespace before comparing |
ignoreCase() |
StringDiff |
Lowercase both inputs before comparing |
ignoreBlankLines() |
StringDiff |
Remove blank lines before comparing |
toUnified(int $context = 3) |
string |
Unified diff format |
toAnsi(int $context = 3) |
string |
ANSI-colored unified diff for terminals |
toHtml() |
string |
HTML with ins/del tags |
toHtmlSideBySide() |
string |
Side-by-side HTML table |
toArray() |
array<DiffLine> |
Array of DiffLine value objects |
hasChanges() |
bool |
Whether any differences exist |
stats() |
DiffStats |
Count of added, removed, unchanged lines |
similarity() |
float |
Ratio of unchanged to total lines (0.0-1.0) |
ArrayDiff
| Method | Returns | Description |
|---|---|---|
changes() |
array<Change> |
All changes |
added() |
array<Change> |
Only added entries |
removed() |
array<Change> |
Only removed entries |
changed() |
array<Change> |
Only modified entries |
hasChanges() |
bool |
Whether any differences exist |
ObjectDiff
| Method | Returns | Description |
|---|---|---|
changes() |
array<PropertyChange> |
All property changes |
hasChanges() |
bool |
Whether any differences exist |
Value Objects
DiffLine-type(added|removed|unchanged),content,lineNumberChange-key,old,new,type(added|removed|changed)PropertyChange-property,from,toDiffStats-added,removed,unchanged
Development
composer install vendor/bin/phpunit vendor/bin/pint --test
Support
If you find this project useful:
License
philiprehberger/php-diff 适用场景与选型建议
philiprehberger/php-diff 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「diff」 「compare」 「unified-diff」 「myers」 「html-diff」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 philiprehberger/php-diff 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philiprehberger/php-diff 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 philiprehberger/php-diff 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.
PHP Semantic Versioning Parser and Comparator
Schema Diff: Show difference between MySQL databases
Small class for checking ip addresses
Submodule for SilverShop which allows you to compare products in a table.
统计信息
- 总下载量: 48
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-15