dongshang1529 2013-10-08 11:29
浏览 50
已采纳

如何使用domdocument php只调用一次UL类

I am using PHP Domdocument to load my html. In my HTML, I have class="smalllist" two times. But, I need to load the first class elements.

Now, My PHP Code is

    $d = new DOMDocument();
    $d->validateOnParse = true;
    @$d->loadHTML($html);
    $xpath = new DOMXPath($d);
    $table = $xpath->query('//ul[@class="smalllist"]');
    foreach ($table as $row) {
       echo $row->getElementsByTagName('a')->item(0)->nodeValue."-";
       echo $row->getElementsByTagName('a')->item(1)->nodeValue."
";

    }

which loads both the classes. But, I need to load only one class with that name. Please help me in this. Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dqbh8054 2013-10-08 12:58
    关注

    This is my final code:

            $d = new DOMDocument();
            $d->validateOnParse = true;
            @$d->loadHTML($html);
            $xpath = new DOMXPath($d);
            $table = $xpath->query('//ul[@class="smalllist"]');
            $count = 0;
            foreach($table->item(0)->getElementsByTagName('a') as $anchor){
               $data[$k][$arr1[$count]] = $anchor->nodeValue;
               if( ++$count > 1 ) {
                  break;
               }
            }
    

    Working fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对