在 PHP 中计算文本差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/112523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate text diffs in PHP
提问by nickf
Are there any libraries (3rd party or built-in) in PHPto calculate text diffs?
是否有任何库(第 3 方或内置)PHP来计算文本差异?
采纳答案by Jonah Braun
What sort of diffs? File diffs? There is array_diff()which acts on arrays. Then there is also xdiff, which "enables you to create and apply patch files containing differences between different revisions of files.". The latter acts on files or strings.
什么样的差异?文件差异?有array_diff()作用于数组。然后还有xdiff,它“使您能够创建和应用包含不同文件修订版之间差异的补丁文件。”。后者作用于文件或字符串。
Edit: I should add xdiff doesn't appear to be out in a release yet. You have to build from source to use it.
编辑:我应该添加 xdiff 似乎还没有发布。您必须从源代码构建才能使用它。
回答by Alister Bulman
it depends exactly what you mean and what you want to do but there is
这完全取决于你的意思和你想做什么,但有
PEAR Text_Diff - Engine for performing and rendering text diffs
PEAR Text_Diff- 用于执行和呈现文本差异的引擎
回答by takeshin
There is a nice and tiny Simplediff project on Githubwhich creates text and HTML diffs.
Github 上有一个不错的小型Simplediff 项目,可以创建文本和 HTML 差异。
回答by mlambie
回答by Mathew Byrne
It really depends on what outcome you want. If all you want to do is to get the diff files for two sets of text, you may find it simpler to just use an external diff command (which of course totally depends on the environment you're developing for).
这真的取决于你想要什么结果。如果您只想获取两组文本的 diff 文件,您可能会发现使用外部 diff 命令更简单(这当然完全取决于您正在开发的环境)。
$diff = `diff $file1 $file2`;
From there if you want to use the diff information at all you would need to parse and this solution might not be what you're after. In that case I'd suggest looking at the PEAR library mentioned above or searching for a similar text parsing library.
从那里如果你想使用差异信息,你需要解析,这个解决方案可能不是你所追求的。在这种情况下,我建议查看上面提到的 PEAR 库或搜索类似的文本解析库。
回答by ceejayoz
MediaWiki's diff engine is open source (just like the rest of it). If you like the way Wikipedia handles text diffs, it may be a solution for you.
MediaWiki 的差异引擎是开源的(就像它的其余部分一样)。如果您喜欢 Wikipedia 处理文本差异的方式,它可能是您的解决方案。
回答by Lou Franco
回答by Aeolun
Not build in, but I like it because it has such a nice interface to test everything out on the website, and because it seems to be somewhat faster than Text_Diff at high granularity levels.
不是内置的,但我喜欢它,因为它有一个很好的界面来测试网站上的所有内容,并且因为它在高粒度级别上似乎比 Text_Diff 快一些。

