I once wrote this diff toolset in PHP to compare edited database records. The algorithm is very simple and fast and works best with short values, e.g. from a database table with many fields. The core function compares two strings (for example two fields from two versions of an edited database record) and returns three numbers, indicating the position and length of the two substrings that are different in both strings. If only a single character was changed, this particular character is highlighted. If a line contains multiple changes, everything from the first to the last different character is highlighted.
Click here to see it in action.
Please note that it does not work in Internet Explorer 9 or below. Replace some []
with .charAt()
to fix this.
Save 1 byte.