vi-kon/laravel-diff

此包已被废弃且不再维护。未建议替代包。

Laravel 5 的 Diff 工具

v1.0.2 2016-01-09 22:37 UTC

This package is not auto-updated.

Last update: 2021-03-05 22:44:56 UTC


README

此包用于比较字符串并显示变更。

目录

返回顶部

功能

  • 比较 字符串
  • 比较 文件
  • 将字符串差异分组为 块组

安装

通过 composer

composer require vi-kon/laravel-diff

返回顶部

用法

简单用法

// Compare string line by line
$diff = Diff::compare("hello\na", "hello\nasd\na");
// Outputs span, ins, del HTML tags, depend if entry
// is unmodified, inserted or deleted
echo $diff->toHTML();

比较两个文件

// Compare files line by line
$diff = Diff::compareFiles("a.txt", "b.txt");
echo $diff->toHTML();

您可以通过获取原始数据来自定义输出

$options = [
    // Compare by line or by characters
    'compareCharacters' => false,
    // Offset size in hunk groups
    'offset'            => 2,
];

$diff = Diff::compare("hello\na", "hello\nasd\na", $options);
$groups = $diff->getGroups();

foreach($groups as $i => $group)
{
    // Output: Hunk 1 : Lines 2 - 6
    echo 'Hunk ' . $i . ' : Lines ' 
         . $group->getFirstPosition() . ' - ' . $group->getLastPosition(); 
    
    // Output changed lines (entries)
    foreach($group->getEntries() as $entry)
    {
        // Output old position of line
        echo $entry instanceof \ViKon\Diff\Entry\InsertedEntry 
            ? '-'
            : $entry->getOldPosition() + 1;

        echo ' | ';

        // Output new position of line
        echo $entry instanceof \ViKon\Diff\Entry\DeletedEntry 
            ? '-'
            : $entry->getNewPosition() + 1;
        
        echo ' - ';        

        // Output line (entry)
        echo $entry;
    }
}

返回顶部

许可证

此包使用 MIT 许可证授权

返回顶部