2014-04-13 31 views
1

我試圖抓住TBODY內的所有的TR從下面的表

<table id="a"> 
    <thead> 
     <tr>empty</tr> 
    </thead> 
    <tbody> 
     <tr> 
      content I want is here 
     </tr> 
    </tbody> 
</table> 

挑TR但它不會忽略空的tr在thead。有沒有人有辦法解決嗎?

回答

2

這確實是一個奇怪的行爲...即使使用>意味着直接的孩子,不工作!

檢查:

$input = <<<_DATA_ 
    <table id="a"> 
     <thead> 
      <tr>empty</tr> 
     </thead> 
     <tbody> 
      <tr> 
       content I want is here 
      </tr> 
     </tbody> 
    </table> 
_DATA_; 

//Create a DOM object 
$html = new simple_html_dom(); 
// Load HTML from a string 
$html->load($input); 


foreach($html->find('tbody > tr') as $tr){ 
    // The parent tag name 
    $parentTag = $tr->parent()->tag; 

    echo $parentTag . ' => ' . $tr->plaintext; 

    // Make sure the parent tag is 'tbody' 
    if($parentTag == 'tbody') 
     echo ' => OK'; 

    echo '<br>'; 
} 

OUTPUT

thead => empty 
tbody => content I want is here => OK 

因此,一個workarround將是檢驗如果父等於如上述CHOWN

適當的值