Skip to content

Instantly share code, notes, and snippets.

@kylebragger
Created October 28, 2009 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylebragger/220443 to your computer and use it in GitHub Desktop.
Save kylebragger/220443 to your computer and use it in GitHub Desktop.
<?php
/*
Paul's Simple Diff Algorithm v 0.1
(C) Paul Butler 2007 <http://www.paulbutler.org/>
May be used and distributed under the zlib/libpng license.
This code is intended for learning purposes; it was written with short
code taking priority over performance. It could be used in a practical
application, but there are a few ways it could be optimized.
Given two arrays, the function diff will return an array of the changes.
I won't describe the format of the array, but it will be obvious
if you use print_r() on the result of a diff on some test data.
htmlDiff is a wrapper for the diff command, it takes two strings and
returns the differences in HTML. The tags used are <ins> and <del>,
which can easily be styled with CSS.
*/
// From http://compsci.ca/v3/viewtopic.php?p=142539
// Patched up by Kyle Bragger to fix PHP notices
function diff($old, $new){
$maxlen = 0;
$ret = 0;
foreach($old as $oindex => $ovalue){
$nkeys = array_keys($new, $ovalue);
foreach($nkeys as $nindex){
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if($matrix[$oindex][$nindex] > $maxlen){
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
return array_merge(
diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
function htmlDiff($old, $new){
$ret = '';
$diff = diff(explode(' ', $old), explode(' ', $new));
foreach($diff as $k){
if(is_array($k))
$ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
(!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
else $ret .= $k . ' ';
}
return $ret;
}
$st = microtime(true);
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
echo htmlDiff('something is really interesting about this', 'Something is very interesting to me in this code');
$et = microtime(true);
echo "\ndone in " . ($et - $st);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment