This question already has an answer here:
Are there any libraries (3rd party or built-in) in PHP
to calculate text diffs?
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.
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
There is a nice and tiny Simplediff project on Github which creates text and HTML diffs.
The output of this is in GNU diff format. It might be what you're looking for.
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.
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.
I really like this JavaScript based one for web projects.
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.
来源:https://stackoverflow.com/questions/112523/calculate-text-diffs-in-php