2011-02-12 59 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 

,但我有一個像150,其中INT的實例。在我的文件中找到,所以我希望得到它顯示是這樣的:

<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循環,但我忘了他們的關鍵價值,我的循環是有點差異,但我認爲我是在正確的領域。嗯,只是好奇,但它返回Array – Eli 2011-02-12 12:08:48