2012-03-16 79 views
0

我有一個PHP DOM對象 http://php.net/manual/en/class.domdocument.php如何使用PHP DOM對象提取一些內容?

難道僅僅可以顯示從第三標籤,並在該表中的第二個標籤內容有問題?

/*** a new dom object ***/ 
$dom = new domDocument; 

/*** load the html into the object ***/ 
@$dom->loadHTML($html); 

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table'); 

/*** get all rows from the table ***/ 
$rows = $tables->item(0)->getElementsByTagName('tr'); 

/*** loop over the table rows ***/ 
foreach ($rows as $row) 
{ 
    /*** get each column by tag name ***/ 
    $cols = $row->getElementsByTagName('td'); 

    /*** echo the values ***/ 
    echo $cols->item(0)->nodeValue.'<br />'; 
    echo $cols->item(1)->nodeValue.'<br />'; 
    echo $cols->item(2)->nodeValue.'<br />'; 
    echo $cols->item(3)->nodeValue.'<br />'; 
    echo $cols->item(4)->nodeValue.'<br />'; 
    echo $cols->item(5)->nodeValue.'<br />'; 
    echo '<hr />'; 
} 

編輯:

我得到這個錯誤:致命錯誤:在

<?php 

/*** a new dom object ***/ 
$dom = new domDocument; 

/*** load the html into the object ***/ 
@$dom->loadHTML('content.html'); 

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

$xpath = new DOMXPath($dom); 

$selected = $xpath->query('//table/tr/td[first()+1]'); 
echo $selected[0]->nodeValue; 
?> 

EDIT2無法使用類型的DOMNodeList的對象數組:

<?php 

$output = file_get_contents('test.php'); 

/*** a new dom object ***/ 
$dom = new domDocument; 

/*** load the html into the object ***/ 
@$dom->loadHTML($output); 

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table');//get all the tables 

if($tables->length > 2) { //check there are more than 2 

    $thirdTable = $tables->item(2); 

    $cols = $thirdTable->getElementsByTagName('td'); 

    /*** echo the values ***/ 
    echo $cols->item(0)->nodeValue.'<br />'; 
    echo $cols->item(1)->nodeValue.'<br />'; 
    echo $cols->item(2)->nodeValue.'<br />'; 
    echo $cols->item(3)->nodeValue.'<br />'; 
    echo $cols->item(4)->nodeValue.'<br />'; 
    echo $cols->item(5)->nodeValue.'<br />'; 
    echo '<hr />'; 
} 

?> 

EDIT3 - 此代碼僅顯示來自第三個表格標籤的內容。但它也只需要顯示第三個表格中第二個tr標籤的內容。

$html = file_get_contents('content.html'); 

/*** a new dom object ***/ 
$dom = new domDocument; 

/*** load the html into the object ***/ 
@$dom->loadHTML($html); 

/*** discard white space ***/ 
$dom->preserveWhiteSpace = false; 

/*** the table by its tag name ***/ 
$tables = $dom->getElementsByTagName('table'); 

/*** get all rows from the table ***/ 
$rows = $tables->item(2)->getElementsByTagName('tr')->item(1); 

/*** loop over the table rows ***/ 
foreach ($rows as $row) 
{ 
    /*** get each column by tag name ***/ 
    $cols = $row->getElementsByTagName('td'); 

    /*** echo the values ***/ 
    echo $cols->item(0)->nodeValue.'<br />'; 
    echo $cols->item(1)->nodeValue.'<br />'; 
    echo $cols->item(2)->nodeValue.'<br />'; 
    echo $cols->item(3)->nodeValue.'<br />'; 
    echo $cols->item(4)->nodeValue.'<br />'; 
    echo $cols->item(5)->nodeValue.'<br />'; 
    echo '<hr />'; 
} 
+0

我在$ HTML變量HTML內容。 – user1273409 2012-03-16 07:24:56

+0

所述第一語法錯誤,這是因爲[],使用 - >項(0),而不是支架 – artragis 2012-03-16 18:53:15

回答

2

我不明白你的問題。用$cols->item(2)你得到了你需要的第二個DOME組件。

如果你只是想第一(或第二......),你可以使用XPath

$xpath = new DOMXpath($document); 
$selected = $xpath->query('//table/tr/td[first()+1] | //table/tr/td[first()+2]'); 
echo $selected[0]->nodeValue; 

如果你不想使用DOMXPath,你可以留在你的getElementsByTagName 首先,你得到的所有表 則檢查有超過2 則採取第三 然後 你保持在陣列中第二你把TR元件和第三

$tables = $dom->getElementsByTagName('table');//get all the tables 
if($tables->length > 2){//check there are more than 2 
    $thirdTable = $tables->item(2); 
    //get the tr then td 
} 
+0

是否有可能使用DOMXpath用foreach? 我需要它來對錶中的每個進行foreach。 – user1273409 2012-03-16 08:41:08

+0

我給你的查詢返回每個表的每個tr的每第二個和第三個TD。返回值是遍歷foreach – artragis 2012-03-16 08:48:25

+0

對不起,但我只需要從一個表中的和​​- 第三個。 – user1273409 2012-03-16 08:55:35

1

您正在嘗試在DOMNodeList上使用foreach。這是一個對象而不是數組。你需要使用一個for loop遍歷這樣的:

$tables = $dom->getElementsByTagName('table'); 
if($tables->length < 3) { 
    // Ahh crap! There is no third table! 
} 
$thirdTable = $tables->item(2); 
$rows = $thirdTable->getElementsByTagName('tr'); 
for($i = 0; $i < $rows->length; $i++) { 
    $row = $rows->item($i); 
    $cols = $row->getElementsByTagName('td'); 
    $secondTd = $row->item(1); 
    $thirdTd = $row->item(2); 
} 
+0

好的,但其他兩個表跳過怎麼辦? – user1273409 2012-03-16 18:22:40

+0

我更新了它,以顯示如何獲得第三個表格。如果您打算繼續進行PHP開發,我強烈建議您閱讀PHP手冊(如我在某些地方的答案中所鏈接的內容)一樣舒適。它會幫助你回答很多這類問題。 – Marshmellow1328 2012-03-16 18:41:41

+1

DomNodeList是可遍歷的 – artragis 2012-03-16 18:51:53