dongyan7950 2018-04-20 06:22
浏览 38
已采纳

PHP:Simple DOM Parser如何迭代这个html代码

<div id="score">
               <div class="name"><span style="width:68%">A</span></div>
                   <span class="roll">1</span>    

                <div class="name"><span style="width:60%">B</span></div>
                   <span class="roll">2</span>       

                 <div class="name"><span style="width:56%">C</span></div>
                   <span class="roll">3</span>          
 </div>

i want to iterate over each span of div.name but i am not getting to next span.roll tag I have used this code yet and also check the condition if A is available then 1 display and same as above name to check.

<?php
include("simple_html_dom.php");
$obj = new simple_html_dom();

 foreach ($obj->find('div[id=score]') as $factor)
    {
         $item = $factor->find('div[class=name] span')->plaintext;

          if(trim($item) == 'A')
          { 
             $a = $factor->find('span[class=roll]',0)->plaintext;
          }
          if(trim($item) == 'B')
          { 
            $b = $factor->find('span[class=roll]',1)->plaintext;
          }
          if(trim($item) == 'C')
          { 
             $c = $factor->find('span[class=roll]',2)->plaintext;
          }
           $final_array['overalldata'] = array
           (
            'a'=> $a,
            'b' => $b,
            'c' => $c,

            );
    }
   print_r($final_array);
   die;


?>

Any body having any idea please help to sort it out. Thanks

  • 写回答

1条回答 默认 最新

  • donglian4464 2018-04-20 06:36
    关注

    As an alternative, since you're targeting that ID, you don't need to have a foreach on the parent element, just get it outright.

    Then you apply the foreach on its children, getting the names and rolls.

    Here's the idea:

    $final_array['overalldata'] = null; // initialize
    $factor = $obj->find('div[id=score]', 0); // get the parent
    foreach ($factor->find('div[class=name]') as $item) { // each item of the parent
        $name = $item->find('span', 0)->innertext; // get the name
        $roll = $item->next_sibling()->innertext; // get the next sibling which is the roll
        $final_array['overalldata'][$name] = $roll; // and assign it (push it inside the array)
    
    }
    print_r($final_array);
    

    So basically, to get the name just target the child inside each div, and then, to target the roll (which is the sibling of each div), just use ->next_sibling() method.

    Sidenote: If I were you, I'd stick to DOM. It's already built-in inside PHP, no need to include any third party libraries.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?