views:

6509

answers:

4

Hi,

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.

Thanks in advance.

+2  A: 

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.

Peter Bailey
+8  A: 

If you want a robust library, Text_Diff (a PEAR package) looks to be pretty good. It has some pretty cool features.

Wickethewok
PHP Inline-Diff, mentioned above, "..uses Text_Diff from PEAR to compute a diff". :)
Mohit Nanda
+9  A: 

You can use PHP Inline-Diff package. It suits your needs, and is quite customisable as well.

and Its given under GPL, so Enjoy!

Mohit Nanda
A: 

This is a nice one, also http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/

Softy
Thanks - that was really handy script.
JW