这里有必要简单说明一下:在统一格式中,加号(+)前缀表示增加的行,减号(-)前缀表示删除的行,没有前缀则表示行没有变化。将上面的输出结果与原始文件进行比较,很容易看出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迅速有效地比较文件内容,以及通过格式化输出结果来增强可读性。祝编码快乐!