Jump to content

How can I test these two functions - diff algorithm from Paul Butler


sql-lover

Recommended Posts

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,

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.