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:
- Computing the difference of the files
- 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:
<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<title>PEAR Bug <del>Tracking</title></del><ins>Tracker</title></ins>
</head>
<body>
<h1>PEAR Bug Tracking System</h1>
<div <del>class="bug-box"></del><ins>class="bug-box bug-box-first"></ins>
<h2>Report <del>new</del><ins>New</ins> Bug</h2><del>
<p>Got</del><ins>
<p>
Got</ins> a test <del>case?</p>
<p>Reproducable steps?</p></del><ins>case or reproducible steps?
</p></ins>
</div>
</body>
</html>
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: