2012-06-18 98 views
0

可能重複:
How do you parse and process HTML/XML in PHP?讀內容在HTML div標籤PHP

目前,我有一個代碼片段看起來像這樣:

echo '<div id="content">'; 
    echo '<b>Results:<br></b>'; 
    echo '<div style="margin-right: 230px; margin-top: -20px;"><center><a href="http://clubpenguincheatsnow.com/tools/itemdatabase/"><b>Back</b></a><center></div>'; 
    $string = explode('<br>', $string); 
    foreach ($string as $row) { 
     preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches); 
     if (preg_match("/$query/i", "$matches[1]")) { 
      echo "<a href='http://clubpenguincheatsnow.com/tools/itemdatabase/info.php?id=$matches[2]'>"; 
      echo $matches[1]; 
      echo "</a><br>"; 
     } 
    } 
    echo '</div>'; 

正如你可以看到我有頂部和底部的div標籤echo '<div id="content">';echo '</div>';。我想要做的是我的PHP代碼來掃描兩個標籤之間的輸出,如果文本<b>Results:<br></b><div style="margin-right: 230px; margin-top: -20px;"><center><a href="http://clubpenguincheatsnow.com/tools/itemdatabase/"><b>Back</b></a><center></div>位於標籤之間,我希望我的代碼輸出「Test」。任何有關我的問題的幫助將是非常有益的!

+2

使用DOM解析器來處理HTML,而不是正則表達式! – Brad

+0

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Crontab

回答

-1

如果您對Regex感到滿意,可以在頁面保持靜態時使用它。 This網站可以幫助您構建正則表達式查詢。否則,請儘可能使用簡單的HTML DOM,如上所述。你會有更少的機會獲得錯誤。

相關問題