定制 yusitnikov/php-diff 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

yusitnikov/php-diff

Composer 安装命令:

composer require yusitnikov/php-diff

包简介

Text diff library for PHP

README 文档

README

Text diff library for PHP

Installation

composer require yusitnikov/php-diff

Usage

Create an instance of LevenshteinDiffCalculator. Arguments:

  1. string $separatorRegex - separator RegEx that will split the strings into units. By default, strings will be split into letters.
  2. OperationCostCalculator $operationCostCalculator - specify it to apply custom operation costs. By default, the cost for each operation (insert, delete, replace) will be 1.
  3. StringDiffCalculatorInterface $itemDiffCalculator - diff calculator that would be applied when replacing a unit.

Example 1. Simple use-case - calculate distance and diff letter by letter.

use Chameleon\PhpDiff\LevenshteinDiffCalculator;

$s1 = 'Levenshtein';
$s2 = 'Einstein';

$differ = new LevenshteinDiffCalculator();
var_dump($differ->calcDistance($s1, $s2));
var_dump($differ->calcDiff($s1, $s2));

Output:

int(5)
class Chameleon\PhpDiff\StringDiffResult#14 (2) {
  public $distance =>
  int(5)
  public $diff =>
  array(5) {
    [0] =>
    class Chameleon\PhpDiff\StringDiffOperation#12 (2) {
      public $operation =>
      string(6) "DELETE"
      public $content =>
      string(4) "Leve"
    }
    [1] =>
    class Chameleon\PhpDiff\StringDiffOperation#13 (2) {
      public $operation =>
      string(6) "INSERT"
      public $content =>
      string(2) "Ei"
    }
    [2] =>
    class Chameleon\PhpDiff\StringDiffOperation#7 (2) {
      public $operation =>
      string(5) "MATCH"
      public $content =>
      string(2) "ns"
    }
    [3] =>
    class Chameleon\PhpDiff\StringDiffOperation#5 (2) {
      public $operation =>
      string(6) "DELETE"
      public $content =>
      string(1) "h"
    }
    [4] =>
    class Chameleon\PhpDiff\StringDiffOperation#4 (2) {
      public $operation =>
      string(5) "MATCH"
      public $content =>
      string(4) "tein"
    }
  }
}

The corresponding diff:

- Leve
+ Ei
ns
- h
tein

Example 2. Complex differ - compare lines and words

use Chameleon\PhpDiff\LevenshteinDiffCalculator;
use Chameleon\PhpDiff\OperationCostCalculator;

$s1 = 'And now here is my secret,
a very simple secret:
it is only with the heart that one can see rightly,
what is essential is invisible to the eye.

"The Little Prince", Antoine de Saint-Exupéry';

$s2 = '> So here is my secret,
> its only with heart that somebody can see rightly,
> what is essential is always invisible to the eye.
> I can promise it to you.

- Le Petit Prince, Antoine Marie Jean-Baptiste Roger, comte de Saint-Exupéry';
$lineDiffer = new LevenshteinDiffCalculator(LevenshteinDiffCalculator::SPLIT_WORDS_REGEX);
$textDiffer = new LevenshteinDiffCalculator(
    LevenshteinDiffCalculator::SPLIT_LINES_REGEX,
    (new OperationCostCalculator())->setReplaceDistanceCalculator($lineDiffer),
    $lineDiffer
);
$result = $textDiffer->calcDiff($s1, $s2);

Diff

yusitnikov/php-diff 适用场景与选型建议

yusitnikov/php-diff 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.58k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2019 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yusitnikov/php-diff 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yusitnikov/php-diff 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 22.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-20