2013-03-06 2 views
-1

두 파일의 차이를 확인하는 PHP 4 코드가 있습니다. PHP 4 버전을 사용하는 이전 서버에서는 정상적으로 작동하지만 새 서버에서는 오류가 발생합니다. 예를 들어 :PHP 4 코드는 PHP 5에서 오류가 발생합니다.

$maxlen에는이

정의 그리고 기능적으로는 새 서버에서 작동하지 않습니다. 누구든지 최근 PHP 버전에서이를 어떻게 변경해야하는지 알고 있습니까?

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); 
    } 
+1

설명서를 시도 했습니까? http://php.net/manual/en/faq.migration5.php – Repox

+0

http://stackoverflow.com/questions/2487021/what-is-the-difference-betwen-variable-in-php4-and-php5 – apoq

+0

어디에서'$ maxlen'을 얻고 있습니까? – codingbiz

답변

1

이것은 버전 차이로 인한 것이 아니며, 잘못된 코드입니다. 이전 설치에서 error_reporting이 (가) 취소 되었기 때문에 아마도 보지 못했을 것입니다. PHP4 환경으로 돌아가 error_reportingE_ALL으로 설정하면 대부분 동일한 경고가 표시됩니다.

$maxlen은 하나의 특정 조건이 충족 될 때 정의되기 때문에 다른 조건에서는 절대로 정의되지 않으며 경고를 생성합니다. 함수의 맨 위에 $maxlen을 정의하거나 변수를 참조하기 전에 isset()을 사용하여이 문제를 피할 수 있습니다.

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

을하지만 루프를 통해 처음으로, $maxlen은 아무것도 설정되지 않으므로 이러한 비교는 경고 (안 오류)를 생성 :

+0

예 PHP로 5 모든 일을하지만 작동하지 않았다 –

1

루프가 포함되어 있습니다.

당신은 루프 전에 $maxlen를 초기화하거나 변경해야 하나 :

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

또 다른 문제는 함수에는 $matrix 배열이 없다는 것이다. 전역 변수 인 경우 함수의 시작 부분에

global $matrix; 

이 필요합니다.

+0

나는 PHP 5와 함께 여전히 작동하지 않는 스크립트를 편집했습니다 –

+0

여전히'$ maxlen'은 정의되지 않았다는 경고를 받고 있습니까? – Barmar

관련 문제