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
This is a nice one, also http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/
Solving the problem is not as simple as it seems, and the problem bothered me for about a year before I figured it out. I managed to write my algorithm in PHP, in 18 lines of code. It is not the most efficient way to do a diff, but it is probably the easiest to understand.
It works by finding the longest sequence of words common to both strings, and recursively finding the longest sequences of the remainders of the string until the substrings have no words in common. At this point it adds the remaining new words as an insertion and the remaining old words as a deletion.
You can download the source here: PHP SimpleDiff...