PHP 比较两个文本文件差异 A diff implementation for PHP

 

实例DEMO:http://sources.ikeepstudying.com/diff/

 

Diff是1974年为Unix操作系统发布的文件比较程序的名称.diff现在更常用于指代比较字符串或文件的函数,以及该函数的输出。在此页面上,您可以下载包含diff实现的PHP类。该类可用于在单行代码中生成以下输出:

 

下载Diff

下载下面的文件并将其上传到您的Web服务器。

File Size Description
class.Diff.php 11,230 bytes PHP class

 

比较字符串和文件

比较函数用于比较两个字符串并逐行确定它们之间的差异。将可选的第三个参数设置为true将比较更改为逐个字符。例如:

// include the Diff class
require_once './class.Diff.php';

// compare two strings line by line
$diff = Diff::compare("line1\nline2", "lineA\nlineB");

// compare two strings character by character
$diff = Diff::compare('abcmnz', 'amnxyz', true);

 

compareFiles函数的行为相同,只是它的前两个参数是文件的路径:

// include the Diff class
require_once './class.Diff.php';

// compare two files line by line
$diff = Diff::compareFiles('old.txt', 'new.txt');

// compare two files character by character
$diff = Diff::compareFiles('old.bin', 'new.bin', true);

 

差异数组

调用compare和compareFiles函数的结果是一个数组。数组中的每个值本身都是一个包含两个值的数组。第一个值是true来自其中一个字符串或正在比较的文件的行(或字符,如果第三个参数设置为)。第二个值是以下三个常量之一:

Constant Meaning
Diff::UNMODIFIED 行或字符存在于字符串或文件中
Diff::DELETED 行或字符仅出现在第一个字符串或文件中
Diff::INSERTED 行或字符仅出现在第二个字符串或文件中

输出功能

Diff类包括三个输出函数,它们涵盖了许多用例,通常意味着您不需要直接处理差异数组。

toString函数返回差异的字符串表示形式。第一个参数是difference数组,可选的第二个参数是输出行之间使用的分隔符(默认情况下是换行符)。例如:

// include the Diff class
require_once './class.Diff.php';

// output the result of comparing two files as plain text
echo Diff::toString(Diff::compareFiles('old.txt', 'new.txt'));

 

结果字符串中的每一行都是来自其中一个字符串或文件的行(或字符),前缀为两个空格,一个减号和一个空格,或一个加号和一个空格,表示哪个字符串或文件包含线。例如:

  An unmodified line
- A deleted line
+ An inserted line

 

toHTML函数的行为与toString函数类似,不同之处在于未修改,删除和插入的行分别包含在span,del和ins元素中,默认分隔符为<br>。例如:

// include the Diff class
require_once './class.Diff.php';

// output the result of comparing two files as HTML
echo Diff::toHTML(Diff::compareFiles('old.txt', 'new.txt'));

 

toTable函数生成更高级的输出,如本页顶部的示例所示。它返回HTML表的代码,其中列包含两个字符串或文件的文本。每行对应于一组尚未修改的行,或者对应于已从第一个字符串或文件中删除的一组行以及已添加到第二个字符串或文件的一组行。该函数有三个参数:差异数组,在结果HTML的每一行中使用的额外缩进量(默认为没有额外的缩进)和分隔符(默认为<br>)。例如:

// include the Diff class
require_once './class.Diff.php';

// output the result of comparing two files as a table
echo Diff::toTable(Diff::compareFiles('old.txt', 'new.txt'));

 

设置差异表的样式

toTable函数将各种类应用于它返回的代码,包括table元素本身的类“diff”。应至少对表格单元格进行样式设置,使文本显示在顶部,因为相邻单元格可能包含不同数量的文本。如果要比较的字符串或文件是源代码,则应保留空白区域,并且文本应以等宽字体显示。例如:

.diff td{
  vertical-align : top;
  white-space    : pre;
  white-space    : pre-wrap;
  font-family    : monospace;
}

在版本8之前的Internet Explorer中正确显示需要两个空格规则(有关详细信息,请参阅空白处理:从HTML 2.0到CSS3)。有关使用等宽字体的一些重要注意事项,请参阅修复浏览器损坏的等宽字体处理

表中的每个单元格都有四个类别之一:diffUnmodified,diffDeleted,diffInserted和diffBlank。diffBlank类用于删除没有相应插入时出现的空表单元格,反之亦然。在本页顶部的示例中,这些类用于显示红色删除和绿色插入。

原文:http://code.stephenmorley.org/php/diff-implementation/

实例:

<?php

// include the Diff class
require_once './class.diff.php';

$diff = Diff::compareFiles("file1.html", "file2.html");

echo Diff::toTable($diff);


?>

<style>
    .diff td{
        vertical-align : top;
        white-space    : pre;
        white-space    : pre-wrap;
        font-family    : monospace;
    }
    .diff td.diffDeleted{
        background:#FFE0E0;
    }
    .diff td.diffInserted{
        background:#E0FFE0;
    }
</style>

实例DEMO:http://sources.ikeepstudying.com/diff/

 

其他的也可以使用:

PHP-FineDiff

下载:PHP-FineDiff-master

项目地址:https://github.com/gorhill/PHP-FineDiff

用法:

Usage
—–

The simplest way to create a diff of two strings is as follow:

include 'finediff.php';
$opcodes = FineDiff::getDiffOpcodes($from_text, $to_text /, default granularity is set to character */);
// store opcodes for later use...

Later, $to_text can be re-created from $from_text using $opcodes as follow:

include 'finediff.php';
$to_text = FineDiff::renderToTextFromOpcodes($from_text, $opcodes);

If you wish a different granularity from the default one, you can use
one of the provided stock granularity stacks:

FineDiff::$paragraphGranularity
FineDiff::$sentenceGranularity
FineDiff::$wordGranularity
FineDiff::$characterGranularity (default)

A basic HTML renderer is provided:

echo FineDiff::renderDiffToHTMLFromOpcodes($from_text, $opcodes);

Customize
———

It is possible to customize the engine by providing a custom “granularity stack”
at your own risk.

It is also possible to provide a custom renderer through a user supplied callback
function/method:

FineDiff::renderFromOpcodes($from, $opcodes, $callback);

 

本文:PHP 比较两个文本文件差异 A diff implementation for PHP

 

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.