PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Text » Text_Diff » Manual

Generate and display difference analysis between text based files.

Introduction to Text_Diff

Text_Diff helps you generating difference views between two or three text files.

@@ -1,3 +1,3 @@
 This line is the same.
-This line is different in 1.txt
+This line is different in 2.txt
 This line is the same.

Diffs are created in two steps:

  1. Computing the difference of the files
  2. Creating the output of the difference

In Text_Diff, an Engine is used to compute the difference between the files/strings. The packages contains several of them: native, shell, string and xdiff. One should use auto to let Text_Diff decide which one is the best for the system.

The second part, creating the output, is made possible by renderers. Text_Diff comes with context , inline and unified renderers.

Renderer examples

Context renderer

<?php
require_once 'Text/Diff.php';
require_once 'Text/Diff/Renderer/context.php';

$lines1 = file('1.htm');
$lines2 = file('2.htm');

$diff     = new Text_Diff('auto', array($lines1, $lines2));
$renderer = new Text_Diff_Renderer_context();
echo $renderer->render($diff);
?>

generates the following output:

***************
*** 3,13 ****
   <head>
!   <title>PEAR Bug Tracking</title>
   </head>
   <body>
    <h1>PEAR Bug Tracking System</h1>
!   <div class="bug-box">
!    <h2>Report new Bug</h2>
!    <p>Got a test case?</p>
! 
!    <p>Reproducable steps?</p>
    </div>
   </body>
  </html>
--- 3,13 ----
   <head>
!   <title>PEAR Bug Tracker</title>
   </head>
   <body>
    <h1>PEAR Bug Tracking System</h1>
!   <div class="bug-box bug-box-first">
!    <h2>Report New Bug</h2>
!    <p>
!     Got a test case or reproducible steps?
!    </p>
    </div>
   </body>
  </html>

Inline renderer

<?php
require_once 'Text/Diff.php';
require_once 'Text/Diff/Renderer/inline.php';

$lines1 = file('1.htm');
$lines2 = file('2.htm');

$diff     = new Text_Diff('auto', array($lines1, $lines2));
$renderer = new Text_Diff_Renderer_inline();
echo $renderer->render($diff);
?>

generates the following output:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;PEAR Bug <del>Tracking&lt;/title&gt;</del><ins>Tracker&lt;/title&gt;</ins>
 &lt;/head&gt;
 &lt;body&gt;
  &lt;h1&gt;PEAR Bug Tracking System&lt;/h1&gt;
  &lt;div <del>class=&quot;bug-box&quot;&gt;</del><ins>class=&quot;bug-box bug-box-first&quot;&gt;</ins>
   &lt;h2&gt;Report <del>new</del><ins>New</ins> Bug&lt;/h2&gt;<del>
   &lt;p&gt;Got</del><ins>
   &lt;p&gt;
    Got</ins> a test <del>case?&lt;/p&gt;

   &lt;p&gt;Reproducable steps?&lt;/p&gt;</del><ins>case or reproducible steps?
   &lt;/p&gt;</ins>
  &lt;/div&gt;
 &lt;/body&gt;
&lt;/html&gt;

Unified renderer

<?php
require_once 'Text/Diff.php';
require_once 'Text/Diff/Renderer/unified.php';

$lines1 = file('1.htm');
$lines2 = file('2.htm');

$diff     = new Text_Diff('auto', array($lines1, $lines2));
$renderer = new Text_Diff_Renderer_unified();
echo $renderer->render($diff);
?>

generates the following output:

@@ -3,13 +3,13 @@
  <head>
-  <title>PEAR Bug Tracking</title>
+  <title>PEAR Bug Tracker</title>
  </head>
  <body>
   <h1>PEAR Bug Tracking System</h1>
-  <div class="bug-box">
-   <h2>Report new Bug</h2>
-   <p>Got a test case?</p>
-
-   <p>Reproducable steps?</p>
+  <div class="bug-box bug-box-first">
+   <h2>Report New Bug</h2>
+   <p>
+    Got a test case or reproducible steps?
+   </p>
   </div>
  </body>
 </html>

Usage examples

Colorized diff output on the shell

Together with Console_Color, it is possible to use Text_Diff to generate colored diffs on an ANSI terminal.

<?php
require_once 'Console/Color.php';
require_once 'Text/Diff.php';
require_once 'Text/Diff/Renderer/inline.php';

$lines1 = file('1.htm');
$lines2 = file('2.htm');

$diff     = new Text_Diff('auto', array($lines1, $lines2));
$renderer = new Text_Diff_Renderer_inline(
    array(
        'ins_prefix' => '%g',
        'ins_suffix' => '%n',
        'del_prefix' => '%r',
        'del_suffix' => '%n',
    )
);
echo Console_Color::convert(
    htmlspecialchars_decode(
        $renderer->render($diff)
    )
);
?>

is displayed like that on a shell: