dongshijiao6890 2016-06-04 06:39 采纳率: 0%
浏览 65
已采纳

使用PHP Simple DOM Parser进行递归

For some reason I get recursion while using Simple DOM Parser Library.

My HTML is like

<div id="root">
    <div class="some_div">some text</div>
    <div class="field_1 misc1"><a href="#">Some text link</a> <strong>15</strong></div>
    <div class="field_2 misc2"><a href="#">Some text link</a> <strong>25</strong></div>
</div>

I created PHP script, included single file

include_once('simple_html_dom.php');

And I try to get 15 and 25 values from HTML above. But when I run

$ret = $html->find('div[id=root]'); 
print_r($ret);

my script returns a lot of recursions - what am i doing wrong and how can i get this 15 and 25 values properly?

  • 写回答

1条回答 默认 最新

  • doudouxuqh198138 2016-06-04 06:46
    关注

    Don't use print_r() or var_dump() on DOM objects. The DOM object has properties that refer to its children and parent. So when it prints the child element, it then needs to print its parent property. And when it prints the parent, it needs to print the child again, so it gets into an infinite recursion.

    If you want to get 15 and 25, you should use a selector that matches those elements. Then loop through the results and print the text.

    $ret = $html->find('#root strong');
    foreach ($ret as $field) {
        echo $field->plaintext;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么