`
天梯梦
  • 浏览: 13729723 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

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

 
阅读更多

Selection_034

 

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

 

Download Diff

Download the file below and upload it to your web server.

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

 

Comparing strings and files

The compare function is used to compare two strings and determine the differences between them on a line-by-line basis. Setting the optional third parameter to true will change the comparison to be character-by-character. For example:

// 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);

 

 

The compareFiles function behaves identically, except that its first two parameters are paths to files:

// 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);

 

 

The differences array

The result of calling the compare and compareFiles functions is an array. Each value in the array is itself an array containing two values. The first value is a line (or character, if the third parameter was set to true) from one of the strings or files being compared. The second value is one of the following three constants:

 

Constant Meaning
Diff::UNMODIFIED The line or character is present in both strings or files
Diff::DELETED The line or character is present only in the first string or file
Diff::INSERTED The line or character is present only in the second string or file

 

Output functions

The Diff class includes three output functions, which cover many use cases and often mean you will not need to process the differences array directly.

 

The toString function returns a string representation of the differences. The first parameter is the differences array, and the optional second parameter is the separator to use between lines of the output (by default, the newline character). For example:

// 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'));

 

 

Each line in the resulting string is a line (or character) from one of the strings or files being compared, prefixed by two spaces, a minus sign and a space, or a plus sign and a space, indicating which string or file contained the lines. For example:

  An unmodified line
- A deleted line
+ An inserted line

The toHTML function behaves similarly to the toString function, except that unmodified, deleted, and inserted lines are wrapped in span, del, and ins elements respectively, and the default separator is <br>. For example:

// 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'));

 

The toTable function produces a more advanced output, as shown in the example at the top of this page. It returns the code for an HTML table whose columns contain the text of the two strings or files. Each row corresponds either to a set of lines that have not been modified, or to a set of lines that have been deleted from the first string or file and a set of lines that have been added to the second string or file. The function takes three parameters: the differences array, an amount of extra indentation to use in each line of the resulting HTML (which defaults to no extra indentation), and a separator (which defaults to <br>). For example:

// 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'));

 

Styling the differences table

The toTable function applies various classes to the code it returns, including the class ‘diff’ on the table element itself. At a minimum the table cells should be styled so that text appears at the top, as neighbouring cells may contain differing amounts of text. If the strings or files being compared are source code, white space should be preserved and the text should be shown in a monospace typeface. For example:

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

 

The two white-space rules are required for correct display in Internet Explorer prior to version 8 (see White space handling: from HTML 2.0 to CSS3 for more details). See Fixing browsers’ broken monospace font handling for some important considerations when using monospace typefaces.

Each cell in the table has one of four classes: diffUnmodified, diffDeleted, diffInserted, and diffBlank. The class diffBlank is used for the empty tables cells that occur when a deletion does not have a corresponding insertion, or the other way round. In the example at the top of this page these classes are used to show deletions in red and insertions in green.

 

原文: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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    jsdiff一个javascript的文本差异比较实现

    在软件开发中,特别是在版本控制系统如Git、SVN等中,文本差异比较是一项重要的功能,它能帮助开发者识别两个文本之间的差异,便于理解和合并代码更改。jsdiff库提供了API接口,使得在Web应用中进行文本比较变得...

    simple-php-diff:查找PHP中两个文本文件之间的快速区别

    该库非常快速地比较两个文本文件,并返回具有差异的对象。 差异处有编号的行,以便于向用户轻松显示更改。 在浏览更改时,您还可以从Diff对象中将更改后的行的数组读取为整数数组。 如何使用 只需创建一个...

    diff diff算法实践

    diff算法,源自Unix操作系统中的一个工具,用于比较两个文本文件的差异,找出它们的相似和不相同之处。在IT行业中,diff算法被广泛应用于版本控制系统、代码审查、文档编辑等领域,它能高效地识别并显示文本内容的...

    前端HTML内容差异比较diff-match-patch.js

    前端HTML内容差异比较diff-match-patch.js

    PHP的差异比较Diff实现从PHPUnit中分解为一个独立的组件

    `sebastianbergmann/diff`库就是这样一个专门用于进行差异比较的独立组件,它原本是PHPUnit测试框架的一部分,但现在已被提取出来,可以单独使用。这个组件提供了强大的功能,帮助开发者直观地看到两个数组、对象...

    java实现两个word文件进行比较

    在Java编程环境中,实现两个Word文档的比较是一项常见的任务,特别是在文档处理或自动化测试的场景中。本篇文章将深入探讨如何使用Java技术有效地完成这个任务,重点在于理解文档的结构、选择合适的库以及如何标记...

    diff命令 文件比较工具 文本比较工具 cygwin中的文本比较工具

    `diff`的基本使用方式是通过指定两个文件名,如`diff file1.txt file2.txt`,它会逐行比较这两个文件,并突出显示不同之处。如果文件完全相同,`diff`将不会输出任何内容。当存在差异时,它会显示哪一行或哪些行不...

    odd diff check tool,微软内部比较文本及文件夹差异工具

    1. **文本比较**: Odd Diff Check Tool 能够对两个文本文件进行逐行或逐字符的对比,高亮显示不同之处。这对于检查代码修改、查找bug或者合并冲突非常有效。它可以识别新增、删除、修改等变化,并提供直观的界面来...

    Qt5.9 文件比较工具

    在Qt5.9中,文件比较功能主要通过`QTextDocument`和`QDiffEngine`等类实现,它们能够处理文本文件的差异比较,并且可以通过自定义渲染来显示比较结果。 文件夹比较是该工具的一大亮点,它能够遍历指定的文件夹,找...

    这个是比较工具diff_tool

    当您有两个版本的文件,例如源代码文件或文本文件,`diff_tool`可以生成一个详细的报告,指出这两个文件中的哪些行或字符不相同。这种差异显示对于追踪修改历史、合并冲突或者找出错误来源非常有用。 在安装`diff_...

    C# 文本对比算法比较两个字符串的不同

    本文将深入探讨如何在C#中实现文本对比算法,以比较两个字符串的不同,并了解如何利用这些差异进行实际应用。 首先,文本对比的基本目标是识别两个文本之间的异同,这在版本控制、文档编辑、代码审查等场景中非常...

    比较两个文件夹的差异

    如果想比较的不是文本文件而是二进制文件,可以使用 `diff -b` 或 `diff --brief`,它将简单地告诉你两个文件是否相同,而不会列出具体差异。 `dirdiff` 命令: `dirdiff` 是扩展了 `diff` 功能的工具,专门用于...

    diff 算法资料 文本比较 论文

    diff算法是一种在计算机科学领域广泛使用的文本比较方法,它的主要目标是找出两个文本文件之间的差异,从而生成一个最小编辑距离(Minimum Edit Distance, MED)的表示,也就是如何通过最少的插入、删除和替换操作将...

    linux 比较两个文件夹diff不同 (diff命令, md5列表)

    首先,我们来看`diff`命令,它是Linux中一个强大的工具,用于比较两个文件或目录的差异。在比较两个文件夹时,我们可以使用以下选项: 1. `-a` 或 `--text`:即使文件看起来不是文本格式,也把它们当作文本进行逐行...

    快速比对两个txt文件内容是否一致

    在这个场景下,我们可以利用`fileinput`模块读取文件内容,再用`difflib`模块来比较两个文件的差异。 `fileinput`模块允许我们方便地逐行迭代多个文件,而`difflib`模块则提供了`Differ()`类,它能够生成人类可读的...

    diff:一个像众所周知的diff程序一样计算两个文本文件差异的小程序

    本文将围绕标题“diff:一个像众所周知的diff程序一样计算两个文本文件差异的小程序”展开,深入探讨这个Java实现的文本文件差异比较工具。 首先,理解`diff`的基本功能至关重要。`diff`程序的主要任务是识别两个...

    diff命令 比较文件的差异

    diff以逐行的方式,比较文本文件的异同处。如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录 。 语法格式:diff [参数] [目录] 常用参数: -a diff预设只会逐行比较文本文件 -...

    php-htmldiff:一个用于比较两个HTML文件片段并使用简单HTML突出显示差异的库。 包括对复杂列表和表格进行比较的支持

    php-htmldiff是一个用于比较两个HTML文件/片段并使用简单HTML突出显示差异的库。 此HTML Diff实现是从,并已通过新功能,错误修复和对原始代码的增强进行了修改。 有关这些修改的更多信息,请阅读或查看 。 安装 ...

Global site tag (gtag.js) - Google Analytics