Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight differences between two strings

If I have two longish strings, VARCHAR2s, is there a simple method or algorithm I can copy or port to PL/SQL to compare them, inserting markup (i.e. so that when rendered in a web page the differences will be highlighted).

For example:

BEGIN
  DBMS_OUTPUT.put_line(
    markup_differences
         (in_old     => 'Hello world, this is your captain speaking.'
         ,in_new     => 'Hello WORLD, this is not your captain.'
         ,in_preins  => '<ins>'
         ,in_postins => '</ins>'
         ,in_predel  => '<del>'
         ,in_postdel => '</del>'
         ));
END;

Expected output:

Hello <del>world</del><ins>WORLD</ins>, this is <ins>not</ins> your captain
<del>speaking</del>.

Notice that this shows that "world' was changed to "WORLD", that "not" was inserted, and that "speaking" was removed.

Background: My intention is to compare two mostly-similar HTML fragments, and mark them up with highlights for display in a browser. Performance will not be a priority. This is for a throwaway app, so I'm not after a perfect solution. Even if something gets me part of the way there will be better than nothing - and I haven't promised anything to the client yet :)

Alternatively, a simple solution in Javascript that I can easily incorporate in my Apex application would be acceptable.

like image 854
Jeffrey Kemp Avatar asked Nov 03 '22 15:11

Jeffrey Kemp


1 Answers

There is a really simple js-diff algorithm on John Resigs Blog: http://ejohn.org/projects/javascript-diff-algorithm/

Maybe that helps.

like image 197
rag Avatar answered Nov 15 '22 08:11

rag