Using PEAR Text_Diff to compare text files
When it becomes necessary to compare two or more text files in UNIX, most developers reach for the diff program. This program, included by default in almost all UNIX distributions, compares the files line by line and displays the changes between them in a number of different output formats.
Though diff originally is a command-line utility, packages replicating its functionality are available for most development environments and languages, including Perl, JSP, and PHP. And so we come to Text_Diff, a PEAR class that makes it possible to compare file contents in the PHP environment and render the output in various formats.
This tutorial will demonstrate this class in action, illustrating how you can use it to dynamically compare file contents with PHP and render the results as a Web page. I'll assume here that you have a working Apache and PHP installation and that the PEAR Text_Diff class has been correctly installed.
Note: You can install the PEAR Text_Diff package directly from the Web, either by downloading it or by using the instructions provided.
Before writing any code, it's necessary to set up the test files we'll be using in this tutorial. These are two simple files, with some deliberate differences that Text_Diff should be able to pick up on. Snippet A is the first file, named data1.txt.
Having set up the files, let's begin with a simple illustration of how Text_Diff works. Start with the script in Snippet C.
The script begins by initializing these two objects, making use of PHP's file() function to extract the contents of each file as a series of arrays. The Text_Renderer() object is then used to render the output in standard diff format, producing output which should be familiar to any UNIX developer:
Now, the output above is not particularly easy to read unless you have lots of experience at decoding diff results. That's why Text_Diff comes with a couple of options to reformat this output into something more readable. These options are accessible as child classes of the Text_Diff_Renderer() object and make it possible to view comparison results in either unified or inline format.
The following script (Snippet D) modifies the previous example to demonstrate unified format:
And here's the output:
Of course, it's possible to make it even more user-friendly -- and that's precisely what inline formatting tries to accomplish. In this format, strikethroughs are used to visually indicate which characters and lines have changed. Snippet E shows you how to use it.
zebraAnd that's about it for this tutorial. Hopefully you now have a clear idea of how Text_Diff can be used to rapidly and efficiently compare files in the PHP environment and how the output can be formatted for easy readability. Happy coding!
|All times are GMT +5.5. The time now is 12:24.|