dpoppu4300
2015-03-14 12:21
浏览 100
已采纳

XPath在TR中选择TD

I want to capture all the content between td tags but divide them by their tr. So i can get an array with the content inside every tr.

<div id="box">
<tr align='center'>
<td>1</td>
<td style='padding-left: 0px !important;padding-right: 10px !important;'>   <div id=''></div></td> 
<td>45</td>
<td>62</td>
</tr><tr align='center'>
<td>2</td>
<td style='padding-left: 0px !important;padding-right: 10px !important;'>   <div id=''></div></td> 
<td>35</td>
<td>47</td>
</tr><tr align='center'>
<td>3</td>
<td style='padding-left: 0px !important;padding-right: 10px !important;'>   <div id=''></div></td> 
<td>63</td>
<td>58</td>
</tr>

I've tried with this:

<?php
$url = '';
$html = file_get_contents($url);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;
@$doc->loadHTML($html);
$xpath = new DOMXpath ($doc);
$expresion = "//div[@id='box']//tr//td";
$node = $xpath->evaluate($expresion);
foreach ($node as $nd)
{
echo $nd->nodeValue;
}
?>

But the output is:

1

45
62
2

35
47
3

63
58
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujia4619 2015-03-15 09:58
    已采纳

    If you want to group the td values by their tr, I would separate the xpath into two queries. One query selects the <tr> nodes and a second query selects the <td> childs of that node.

    If you put that into a loop it can look like this:

    <?php
    
    $html = <<<EOF
    <div id="box">
    
        ... Your HTML comes here
    </tr>
    EOF;
    
    $url = '';
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = FALSE;
    @$doc->loadHTML($html);
    $xpath = new DOMXpath ($doc);
    $expresion = "//div[@id='box']//tr";
    $trs = $xpath->evaluate($expresion);
    foreach ($trs as $tr)
    {
        $tdvals = array();
        foreach($xpath->query('td', $tr) as $td) {
            /* Skip the td with the empty text value */
            if(trim($td->nodeValue) !== '') {
                $tdvals []= $td->nodeValue;
            }
        }
        echo implode(',', $tdvals) . PHP_EOL;
    }
    

    which outputs:

    1,45,62
    2,35,47
    3,63,58
    

    One another thing. In your example you are using file_get_contents() to load the HTML. Note that you can use DOMDocument::loadHTMLFile() to load (remote) files.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题