2014-05-14 2 views
-3

저는 PHP로 아직 완전히 새로운 것 같습니다. 나는이 통보에 관한 다른 게시물을 보았지만 아무도 내 상황에 대해 말하지 않는 것처럼 보였다. 다른 페이지에서 제목 (h2 요소)을 나열하려고합니다.PHP 알림 - 정의되지 않은 오프셋 1

'공지 사항 : 정의되지 않은 오프셋 : 1 /Users/jessenichols/Sites/HCS/news.php on line 26'이 (가) 나열된 해당 작업에 성공한 동안, 당신의 getTitle 방법

<?php 
     function getTitle($Url){ 
      $str = file_get_contents($Url); 
      if(strlen($str)>0){ 
       preg_match("/\<h2\>(.*)\<\/h2\>/",$str,$title); 
       return $title[1]; 
      } 
     } 
     if ($handle = opendir('news')) { 
      while (false !== ($entry = readdir($handle))) { 
       if ($entry != "." && $entry != "..") { 
        echo '<p class="article_selector">'.getTitle('news/'."$entry").'</p>'; 
       } 
      } 
      closedir($handle); 
     } 
    ?> 
+0

배경 info ... Line 26은 'return $ title [1];'줄입니다. – bboysupaman

+4

'$ return [1]'을 할 때'while' 루프의 반복 중 하나에서 정규 표현식이 일치하지 않습니다. 따라서 존재하지 않는 무언가를 반환하려고 시도하고 있습니다. – mituw16

답변

1

$title[1]가 설정되어있는 경우, 경우 다음 while 루프, null을 반환하지 확인 변수에 getTitle()에서 결과를 할당하고이 변수가 null가 아닌 경우이

처럼 확인
<?php 
     function getTitle($Url){ 
      $str = file_get_contents($Url); 
      if(strlen($str)>0){ 
       preg_match("/\<h2\>(.*)\<\/h2\>/",$str,$title); 

       // if $title[1] isn't set, return null 
       return isset($title[1]) ? $title[1] : null; 
      } 
     } 
     if ($handle = opendir('news')) { 
      while (false !== ($entry = readdir($handle))) { 
       if ($entry != "." && $entry != "..") { 
        // first, get the title 
        $title = getTitle('news/'.$entry); 

        // and after check if title is not null 
        if (null != $title) { 
         echo '<p class="article_selector">'.$title.'</p>'; 
        } 
       } 
      } 
      closedir($handle); 
     } 
    ?> 
+1

이것은 정확히 내가 필요한 것입니다! 고마워요! – bboysupaman

관련 문제