站长论坛
标题:
用PHP和PEAR比较文件内容提交输出
[打印本页]
作者:
superadmin
时间:
2007-9-24 22:28
标题:
用PHP和PEAR比较文件内容提交输出
在UNIX中,如果有必要比较两个或多个文本文件,许多开发者借助于diff程序。几乎所有UNIX产品都默认提供这个程序,它逐行比较文件内容,并以各种输出格式显示文件间的差异。
虽然diff最初(现在仍然)是一个命令行实用工具,但大多数开发环境和语言,包括Perl、JSP和PHP都包含复制其功能的软件包。因此我们将讨论Text_Diff——一个在PHP环境中比较文件内容并以各种格式提交输出结果的PEAR类。
本教程将举例说明这个类的用法,阐明如何使用它动态比较PHP文件内容,并以网页的形式提交结果。这里我假设你应用一个Apache和PHP环境,并且已经正确安装PEAR Text_Diff类。
注:你可以下载,或使用这里提供的用法说明,直接从网络上安装PEAR Text_Diff软件包。
创建测试文件
在编写任何代码前,有必要创建我们在本教程中用到的测试文件。这是两个简单的文件,我们在其中有意添加了几个Text_Diff应该能够发现的差异。列表A是第一个文件——data1.txt。
列表A
apple
banana
cantaloupe
drumstick
enchilada
fig
grape
horseradish
列表B是第二个文件——data2.txt。
列表B
apple
bat
cantaloupe
drumstick
enchilada
fig
peach
pear
zebra
进行基本的比较
建立测试文件后,首先我们简单说明Text_Diff的用法。先从列表C的脚本开始:
列表C
<?php
// adjust file paths as per your local configuration!
include_once "Text/Diff.php";
include_once "Text/Diff/Renderer.php";
// define files to compare
$file1 = "data1.txt";
$file2 = "data2.txt";
// perform diff, print output
$diff = &new Text_Diff(file($file1), file($file2));
$renderer = &new Text_Diff_Renderer();
echo $renderer->render($diff);
?>
作者:
superadmin
时间:
2007-9-24 22:28
初看起来,这段脚本相当简单。在Text_Diff软件包中有两个基本的类:Text_Diff(),它执行比较并返回diff输出;和Text_Diff_Renderer(),它把diff输出格式化成一个易于理解的格式。特别要指出的是,Text_Diff()对象必须用进行比较的两个文件的内容(而不是位置)进行初始化。
上述脚本首先初始化这两个对象,利用PHP的file()函数把每个文件的内容提取成一系列的数组。然后用Text_Renderer()对象以标准的diff格式提交输出,生成任何UNIX开发者都熟悉的结果:
2c2
<banana
---
>bat
7,8c7,12
<grape
<horseradish
---
>peach
>pear
>
>
>
>zebra
增加输出结果的可读性
现在,除非你在解码diff结果方面拥有丰富经验,上述的输出结果并不是特别容易理解。因此Text_Diff提供许多选项,把这个输出重新格式化为更容易读取的格式。这些选项包含在Text_Diff_Renderer()对象的子类中,通过它们可以用统一的或内置的格式查看比较结果。
下面的脚本(列表D)对前面的例子进行了一些修改,以说明统一格式:
列表D
<html>
<head></head>
<body>
<pre>
<?php
// adjust file paths as per your local configuration!
include_once "Text/Diff.php";
include_once "Text/Diff/Renderer.php";
include_once "Text/Diff/Renderer/unified.php";
// define files to compare
$file1 = "data1.txt";
$file2 = "data2.txt";
// perform diff, print output
$diff = &new Text_Diff(file($file1), file($file2));
$renderer = &new Text_Diff_Renderer_unified();
echo $renderer->render($diff);
?>
</pre>
</body>
</html>
注意在初始化renderer时调用了适当的子类。
以下为输出结果:
@@ -1,8 +1,12 @@
apple
-banana
+bat
cantaloupe
drumstick
enchilada
fig
-grape
-horseradish
+peach
+pear
+
+
+
+zebra
作者:
superadmin
时间:
2007-9-24 22:28
这里有必要简单说明一下:在统一格式中,加号(+)前缀表示增加的行,减号(-)前缀表示删除的行,没有前缀则表示行没有变化。将上面的输出结果与原始文件进行比较,很容易看出diff如何反映哪一行发生变化,以及变化的内容。
当然,你还可以使输出结果更加易于理解——这正是内置格式的任务。这种格式使用删除线直观显示哪些字符和行发生变化。列表E说明了它的用法。
列表E
<html>
<head></head>
<body>
<pre>
<?php
// adjust file paths as per your local configuration!
include_once "Text/Diff.php";
include_once "Text/Diff/Renderer.php";
include_once "Text/Diff/Renderer/inline.php";
// define files to compare
$file1 = "data1.txt";
$file2 = "data2.txt";
// perform diff, print output
$diff = &new Text_Diff(file($file1), file($file2));
$renderer = &new Text_Diff_Renderer_inline();
echo $renderer->render($diff);
?>
</pre>
</body>
</html>
以下为输出结果:
apple
bananabat
cantaloupe
drumstick
enchilada
fig
grape
horseradishpeach
pear
zebra
以上就是本教程的全部内容。希望现在你已经清楚了解如何在PHP环境中使用Text_Diff迅速有效地比较文件内容,以及通过格式化输出结果来增强可读性。祝编码快乐!
欢迎光临 站长论坛 (http://www.tzlink.com/bbs/)
Powered by Discuz! X3.2