sql-lover Posted February 10, 2012 Share Posted February 10, 2012 Hi, While trying to create my own script that allows you to compare two different files , found this amazing algorithm from Paul Butler function diff($oldhistory, $newhistory){ foreach($oldhistory as $oindex => $ovalue){ $nkeys = array_keys($newhistory, $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'=>$oldhistory, 'i'=>$newhistory)); return array_merge( diff(array_slice($oldhistory, 0, $omax), array_slice($newhistory, 0, $nmax)), array_slice($newhistory, $nmax, $maxlen), diff(array_slice($oldhistory, $omax + $maxlen), array_slice($newhistory, $nmax + $maxlen))); } function htmlDiff($oldhistory, $newhistory){ $diff = diff(explode(' ', $oldhistory), explode(' ', $newhistory)); 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; } This is embarrassing, but how can I test this with two text variables? Where should I wrote the echo line to display some output? I'm new to PHP. Also, my own and extremely simplify idea is using strtoupper and md5 for comparing two texts. Is that a good logic? The main goal of my program is able to compare two MS-SQL store procedures from two different servers and tell if they both are the same of different versions. Thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/256825-how-can-i-test-these-two-functions-diff-algorithm-from-paul-butler/ Share on other sites More sharing options...
digibucc Posted February 10, 2012 Share Posted February 10, 2012 those are functions, so on their own they don't really do anything, they need to be called. and that's not just an echo inside the function. you'll want to keep functions in a separate file and include it in your final script, but for the sake of this test, just put them in a file (with <?php ?>), and call the function at the bottom. <?php function diff($oldhistory, $newhistory){ foreach($oldhistory as $oindex => $ovalue){ $nkeys = array_keys($newhistory, $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'=>$oldhistory, 'i'=>$newhistory)); return array_merge( diff(array_slice($oldhistory, 0, $omax), array_slice($newhistory, 0, $nmax)), array_slice($newhistory, $nmax, $maxlen), diff(array_slice($oldhistory, $omax + $maxlen), array_slice($newhistory, $nmax + $maxlen))); } function htmlDiff($oldhistory, $newhistory){ $diff = diff(explode(' ', $oldhistory), explode(' ', $newhistory)); 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; } $oldhistory = 'oldhistory'; $newhistory = 'newhistory'; var_dump(diff($oldhistory, $newhistory)); var_dump(htmlDiff($oldhistory, $newhistory)); ?> in each case, you would define the old and new history variables, and i have it var_dump as that provides detailed information about the return. you can then figure out how to format it depending on it's type. you can also assign them to variables for another use <?php $diff = diff($oldhistory, $newhistory); $htmlDiff = htmlDiff($oldhistory, $newhistory); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256825-how-can-i-test-these-two-functions-diff-algorithm-from-paul-butler/#findComment-1316679 Share on other sites More sharing options...
sql-lover Posted February 10, 2012 Author Share Posted February 10, 2012 digibucc Thank you so much, appreciate the quick reply! Quote Link to comment https://forums.phpfreaks.com/topic/256825-how-can-i-test-these-two-functions-diff-algorithm-from-paul-butler/#findComment-1316703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.