2014-01-15 4 views
-1

내가 프로젝트 PHP 날씨 2.2.2를 사용하고 싶지만 많은 문제가 여기: 통지 오프셋 undefined가 : 2

function get_languages($type) { 

    static $output = array(); 

    if (empty($output)) { 

     /* I use dirname(__FILE__) here instead of PHPWEATHER_BASE_DIR 
     * because one might use this function without having included 
     * phpweather.php first. */ 
     require(dirname(__FILE__) . '/languages.php'); 

     $dir = opendir(dirname(__FILE__) . '/output'); 
     while($file = readdir($dir)) { 

      // I got a problem in this line below  

      if (ereg("^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$", $file, $regs)) { 

      // After i change it to the line mentioned below i got error undefined offset:2 in line 2 mentioned below 
      // how to correct this error or is there any way to correct this code 

      if(preg_match("#^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$#", $file, $regs)) { 
       $output[$regs[1] . $regs[2]] = $languages[$regs[1] . $regs[2]]; 
      } 
     } 
     closedir($dir); 
    } 

    asort($output); 

    return $output; 
} 
+2

$ regs [2]이 (가) 확실합니까? – Joshua

+0

'var_dump ($ regs);'당신은 답을 얻을 것입니다. – Rikesh

+0

도와 주셔서 고맙습니다 :) – user1991

답변

0

당신은이 알림이 표시 코드 인 $regs[2] 때문에 설정되지 않았습니다.

if(preg_match("#^pw_${type}_([a-z][a-z])(_[A-Z][A-Z])?\.php$#", $file, $regs)) { 
    $output[$regs[1] . (isset($regs[2]) ? $regs[2] : '')] = $languages[$regs[1] . (isset($regs[2]) ? $regs[2] : '')]; 
} 

isset()은 검사 값이 존재하거나 배열 값이 존재하지 않습니다. 빈 문자열이있는 연결이없는 경우 ''

을 사용하고 있기 때문에 그렇습니까? 당신의 정규식에서(_[A-Z][A-Z])?

+0

감사합니다 ... 오류가 지금 제거됩니다 – user1991

+0

당신은 오신 것을 환영합니다 :) –

0

사용이 정규 표현식 :

 if(preg_match("#^pw_${type}_([a-z][a-z])((?:_[A-Z][A-Z])?)\.php$#", $file, $regs)) { 

?:와 그룹은 단지 당신이 그것을에 ? 수정을 적용 할 수 있도록하기 위해 존재, 캡처 그룹되지 않습니다. 다른 그룹에 배치하면 그 내용을 캡처 할 수 있습니다. 선택적 정규 표현식이 일치하지 않으면 빈 문자열이됩니다.