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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示