What is the easiest way to highlight the difference between two strings in PHP?
I'm thinking along the lines of the Stack Overflow edit history page, where new text is in green and removed text is in red. If there are any pre-written functions or classes available, that would be ideal.
Just wrote a class to compute smallest (not to be taken literally) number of edits to transform one string into another string:
http://www.raymondhill.net/finediff/
It has a static function to render a HTML version of the diff.
It's a first version, and likely to be improved, but it works just fine as of now, so I am throwing it out there in case someone needs to generate a compact diff efficiently, like I needed.
Edit: It's on Github now: https://github.com/gorhill/PHP-FineDiff
A php port of Neil Frasers diff_match_patch (Apache 2.0 licensed)
You can use the PHP Horde_Text_Diff package. It suits your needs, and is quite customisable as well.
It's also licensed under the GPL, so Enjoy!
This is a nice one, also http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/
I had terrible trouble with the both the PEAR-based and the simpler alternatives shown. So here's a solution that leverages the Unix diff command (obviously, you have to be on a Unix system or have a working Windows diff command for it to work). Choose your favourite temporary directory, and change the exceptions to return codes if you prefer.
What you are looking for is a "diff algorithm". A quick google search led me to this solution. I did not test it, but maybe it will do what you need.