Calculate text diffs in PHP [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 08:24:25

问题


This question already has an answer here:

  • Highlight the difference between two strings in PHP 12 answers

Are there any libraries (3rd party or built-in) in PHP to calculate text diffs?


回答1:


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.

Edit: I should add xdiff doesn't appear to be out in a release yet. You have to build from source to use it.




回答2:


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




回答3:


There is a nice and tiny Simplediff project on Github which creates text and HTML diffs.




回答4:


The output of this is in GNU diff format. It might be what you're looking for.




回答5:


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 $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.




回答6:


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.




回答7:


I really like this JavaScript based one for web projects.

jsdifflib




回答8:


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.

http://www.raymondhill.net/finediff/viewdiff-ex.php



来源:https://stackoverflow.com/questions/112523/calculate-text-diffs-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!