I\'m trying to diff two strings by phrase, similar to the way that StackOverflow diffs the two strings on the version edits page. What would be an algorithm to do this? Are ther
The algorithm you are looking for is Longest Common Subsequence it does most of the work for you.
The outline is something along these lines.
So for example say you have:
"hello world this is a test"
compared with:
"mister hello world"
The result from the LCS is
Now you sprinkle the special sauce when building up. You join the string together while staying mindful of the previous action. The naive algorithm is just join sections that are the same action.
Finally you transform it to html:
<ins>mister</ins> hello world <del>this is a test</del>
Of course the devil is in the detail: