-1

I have PHP 4 code to check differences of two files, this works fine on old servers with PHP 4 versions, but on new servers I'm getting errors. For example:

$maxlen is no defined

And the functionally is not working on new server. Anyone know how to change this for a recent PHP version?

 function diff($old, $new){ 
            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){ 
    $preg="/[\s,]+/";
        $old=str_replace(">","> ",$old);
        $new=str_replace(">","> ",$new);
        $old=str_replace("<"," <",$old);
        $new=str_replace("<"," <",$new);

        $diff = diff(preg_split($preg, $old),preg_split($preg, $new)); 
        foreach($diff as $k){ 
        if(is_array($k)) 
            $ret .= (!empty($k['d'])?"<div style='BACKGROUND-COLOR: red'>".implode(' ',$k['d'])."</div> ":''). 
            (!empty($k['i'])?"<div style='BACKGROUND-COLOR: yellow'>".implode(' ',$k['i'])."</div> ":''); 
            else $ret .= $k . ' '; 
        } 
        return $ret; 
    } 
    function creatediff($oldurl,$newurl,$diffurl){ 
        $sold= file_get_contents($oldurl);
        $snew= file_get_contents($newurl);
        $diff=htmlDiff($sold,$snew);
        $diff=preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#','$1="'.$newurl.'/$2"',$diff);
        file_put_contents($diffurl,$diff);
    }
hakre
  • 193,403
  • 52
  • 435
  • 836
Salinda Bandara
  • 203
  • 7
  • 20

2 Answers2

1

This is not due to a version difference, it's bad code. You probably had error_reporting turned down/off on your previous install which is likely why you did not see it. Go back to your PHP4 environment, set error_reporting to E_ALL, and you'll probably see most of the same warnings.

Because $maxlen is only defined when one particular if condition is met it is never defined in other cases and generates a warning. You can avoid this by either defining $maxlen at the top of your function, or by using isset() before attempting to reference the variable.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
1

Your loop contains:

if($matrix[$oindex][$nindex] > $maxlen)

but the first time through the loop, $maxlen isn't set to anything, so this comparison generates a warning (not an error).

You should either initialize $maxlen before the loop, or change it to:

if (!isset($maxlen) || $matrix[$oindex][$nindex] > $maxlen)

Another problem is that there's no $matrix array in the function. If that's a global variable, you need:

global $matrix;

at the beginning of the function.

Barmar
  • 741,623
  • 53
  • 500
  • 612