Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function diff($old, $new){
- $matrix = array();
- $maxlen = 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){
- // put space in front and behind tags, or they are seen as a word when replacing the one behind it or in front
- $old = str_replace(array("<",">"), array(" <", "> "), $old);
- $new = str_replace(array("<",">"), array(" <", "> "), $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;
- }
Advertisement
Add Comment
Please, Sign In to add comment