Recursion using PHP Simple DOM Parser

最后都变了- 提交于 2020-01-15 03:10:09

问题


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:


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;
}


来源:https://stackoverflow.com/questions/37627214/recursion-using-php-simple-dom-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!