Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a comparison of 2 html text blocks

I need to take two text blocks with html tags and render a comparison - merge the two text blocks and then highlight what was added or removed from one version to the next.

I have used the PEAR Text_Diff class to successfully render comparisons of plain text, but when I try to throw text with html tags in it, it gets UGLY. Because of the word and character-based compare algorithms the class uses, html tags get broken and I end up with ugly stuff like <p><span class="new"> </</span>p>. It slaughters the html.

Is there a way to generate a text comparison while preserving the original valid html markup?

Thanks for the help. I've been working on this for weeks :[

This is the best solution I could think of: find/replace each type of html tag with 1 special non-standard character like the apple logo (opt shift k), render the comparison with this kind of primative markdown, then revert the non-standard characters back into tags. Any feedback?

like image 753
Steve G. Avatar asked Sep 01 '09 05:09

Steve G.


1 Answers

Simple Diff, by Paul Butler, looks as though it's designed to do exactly what you need: http://github.com/paulgb/simplediff/blob/5bfe1d2a8f967c7901ace50f04ac2d9308ed3169/simplediff.php

Notice in his php code that there's an html wrapper: htmlDiff($old, $new)

(His blog post on it: http://paulbutler.org/archives/a-simple-diff-algorithm-in-php/

like image 139
micahwittman Avatar answered Sep 21 '22 22:09

micahwittman