Highlight the difference between two strings in PHP


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.

9/22/2014 7:24:28 PM

Accepted Answer

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!

1/27/2016 9:00:18 PM

Just wrote a class to compute smallest (not to be taken literally) number of edits to transform one string into another string:


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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow