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 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目