doutao1282 2013-07-18 15:19
浏览 214
已采纳

获取文本td Xpath PHP

I am getting tr of tables and then in loop i want get text of all td fields, look here:

<?
    $lines = $xpath->query("//table[@id='cab_table'] //tr");
       var_dump($lines);// Give me object(DOMNodeList)#11 (1) { ["length"]=> int(6) }


            for( $i = 0; $i < count($lines); $i++) {
                if($i != 0){
                    $tds = $xpath->query('//td', $lines[$i]);
                    $result[$i - 1]['number'] = trim($tds->item(0)->nodeValue);
                    $result[$i - 1]['volume'] = trim($tds->item(1)->nodeValue);
                    $result[$i - 1]['sum'] = trim($tds->item(2)->nodeValue);
                }
            }

            var_dump($result); //Give me NULL
            die();

?>

Why i get NULL?

Now i have:

$lines = $xpath->query("//table[@id='cab_table'] //tr");


            foreach($lines as $line) {
             $tds = $xpath->query('//td', $line);
             $count = $tds->length;

                for($i=0; $i<$count; $i++){

                    echo $tds->item($i)->nodeValue.'<br>';
                    //echo $i.'<br>';


                }

            }

But i want make the next for each tr at loop $result[0] = td[0]; $result[1] = td[1]; $result[2] = td[2]; Can you advise me?

  • 写回答

2条回答 默认 最新

  • doujiaozhao2489 2013-07-18 16:40
    关注
    foreach($lines as $line) {
    
                        for($j=0; $j<=3; $j++) {
    
                         $tds_{$j} = $xpath->query('//td['.$j.']', $line);
                         $tds_{$j} = $xpath->query('//td['.$j.']', $line);
                         $tds_{$j} = $xpath->query('//td['.$j.']', $line);
    
                         $count = $tds_{$j}->length;
    
    
                            for($i=0; $i<$count; $i++){
    
                                $this->result['number'][] = $tds_{$j}->item($i)->nodeValue;
                                $this->result['volume'][] = $tds_{$j}->item($i)->nodeValue;
                                $this->result['code'][] = $tds_{$j}->item($i)->nodeValue;
                                $this->result['start_date'][] = $startDate;
                                $this->result['end_date'][] = $endDate;
    
                            }
    
                        }
    
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘