承接 philiprehberger/php-diff 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

philiprehberger/php-diff

Composer 安装命令:

composer require philiprehberger/php-diff

包简介

Diff strings, arrays, and objects with unified, HTML, and structured output

README 文档

README

Tests Latest Version on Packagist Last updated

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, lineNumber
  • Change - key, old, new, type (added|removed|changed)
  • PropertyChange - property, from, to
  • DiffStats - added, removed, unchanged

Development

composer install
vendor/bin/phpunit
vendor/bin/pint --test

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 philiprehberger/php-diff 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 48
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-15