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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向