2011-02-12 5 views
1

이내에 추가이 내 코드입니다 :PHP는 배열에서 각 결과를 얻을 DIV 태그

$searchfor = array(); 
    $searchfor[0] = 'INT.'; 
    $searchfor[1] = 'EXT.'; 

    // get the file contents, assuming the file to be readable (and exist) 
    $contents = file_get_contents($file); 
    // escape special characters in the query 
    $pattern = preg_quote($searchfor[0], '/'); 
    // finalize the regular expression, matching the whole line 
    $pattern = "/^.*$pattern.*\$/m"; 
    // search, and store all matching occurences in $matches 
    if(preg_match_all($pattern, $contents, $matches)){ 
     echo "<div class='int'>".implode("\n", $matches[0])."</div>\n"; 
    }else{ 
     echo "No matches found"; 
    } 

어떻게 내가하는 div 배열로 결과 및 각 배열 항목에서 걸릴 수 있습니다?

지금 출력이

<div class="int">INT 1</div> 
INT 2 

처럼 보이지만 어디서 INT의 150과 같은 경우가 있습니다. 내 파일에있는 그래서 다음과 같이 보여 그것을 얻을하고자 하였다

<div class="int" id="1">INT 1</div> 
<div class="int" id="2">INT 2</div> 
<div class="int" id="3">INT 3</div> 
<div class="int" id="4">INT 4</div> 
<div class="int" id="5">INT 5</div> 
<div class="int" id="6">INT 6</div> 
<div class="int" id="7">INT 7</div> 
<div class="int" id="8">INT 8</div> 

등등 ...

어떤 제안?

업데이트 된 코드 :

echo '<div id="int-div" style="width: 738px; height: 100%; border: 1px solid #000; padding: 5px; background-color: #FFF;">'; 
    $searchfor = array(); 
    $searchfor[0] = 'INT.'; 
    $searchfor[1] = 'EXT.'; 
    $searchfor[2] = 'I/E.'; 

    // get the file contents, assuming the file to be readable (and exist) 
    $contents = file_get_contents($file); 
    // escape special characters in the query 
    $pattern = preg_quote($searchfor[0], '/'); 
    // finalize the regular expression, matching the whole line 
    $pattern = "/^.*$pattern.*\$/m"; 
    // search, and store all matching occurences in $matches 
    if(preg_match_all($pattern, $contents, $matches)){ 

     $row = 0; 
     foreach($matches as $match) 
     { 
      echo "<ol><li><div class='int' id='".$row."'>".implode("</div></li><li><div class='int' id='".$row."'>", $match)."</div></li></ol>\n"; 
      $row++; 
     } 
    }else{ 
     echo "No matches found"; 
    } 
    echo '</div>'; 

업데이트 결과 :

<ol> 
<li> 
<div class="int" id="0">INT. STRAWBERRY'S BEDROOM - MORNING</div> 
</li> 
<li> 
<div class="int" id="0">INT. PALO TORCIDO HIGH SCHOOL, CLASSROOM - MORNING</div> 
</li> 
<li> 
<div class="int" id="0">INT. DUNKIN' DONUTS - MORNING</div> 
</li> 
<li> 
<div class="int" id="0">INT. DUNKIN' DONUTS - MORNING</div> 
<li> 
<div class="int" id="0">INT. DUNKIN' DONUTS - EARLY MORNING (FLASHBACK)</div> 
</li> 
<li> 
<div class="int" id="0">INT. FUENTES RESIDENCE, KITCHEN - NIGHT (PRESENT)</div> 
</li> 
<li> 

답변

3

foreach를 사용하고 ID와 키를 사용

if(preg_match_all($pattern, $contents, $matches)) 
{ 
    $row = 0; 
    foreach($matches as $match) 
    { 
     echo "<div class='int' id='".$row."'>".implode("\n", $match)."</div>\n"; 
     $row++; 
    } 
}else{ 
     echo "No matches found"; 
} 
+0

아, 내가 foreach는 루프를 시도했지만 나는 그들이 핵심 가치 비트에 대해 잊어 버렸고, 내 루프는 조금 diff되었다.하지만 나는 올바른 영역에 있다고 생각한다. 궁금한데 궁금한 점은 Array – Eli

+0

을 반환합니다. 루프의 원래 /,'var_dump ($ match);'와 똑같은 출력을 가져야하고 내용을 게시하여 업데이트하십시오. – RobertPitt

+0

죄송합니다. 예상대로 게시하도록했습니다. 문제는 $ 키가 항상 0입니다. 새 코드로 업데이트합니다. – Eli