1

How to loop through all nodes of Dom.

I use latest Symfony Crawler.

Simple example:

<?php

function test()
{
    $crawler = new Crawler($html);

    $crawler
        ->filter('body > ul')
        ->each(function (Crawler $node, $i) {
            loop($node);
        });
}

function loop(Crawler $node)
{
    static $counter;

    if ($node->filter('ul')->count() > 0) {
        loop($node);
    }

    $counter++;
    echo PHP_EOL,'Node: ', $counter, PHP_EOL;
}

I get a message:

Fatal error: Maximum function nesting level of '100' reached, aborting! in /.../vendor/symfony/css-selector/XPath/Extension/NodeExtension.php on line 269

HTML for example:

<body>
    <ul>
        <li>
            <ul>
                <li>foo</li>
                <li>bar</li>
            </ul>
        </li>
        <li>baz</li>
    </ul>
    <ul>
        <li>buz</li>
    </ul>
    <ul>
        <li>uaz</li>
    </ul>
</body>

Thanks

4
  • Looks like a stack overflow on your recursive call before you get any trace data. Try putting an if($counter <90) around the call to loop() to quit before a stack overflow. Commented Jun 1, 2015 at 22:23
  • Yep, it loops for example when I put if($counter < 10), if I put if($counter < 90) fails again. Commented Jun 1, 2015 at 22:37
  • Does your trace now show the route it is walking the tree? You should be able to work out why it' snot walking the tree the way you expect. Commented Jun 1, 2015 at 22:42
  • So you run loop with the same node? Isn't it an infinite recursion? Commented Jun 1, 2015 at 23:18

1 Answer 1

2

Increase the value of xdebug.max_nesting_level = 250 in your php.ini: http://xdebug.org/docs/all_settings#max_nesting_level

Sign up to request clarification or add additional context in comments.

2 Comments

Why I should increase nesting level for nesting of 4 nodes?
This is a xdebug error, on a production server you will not get it, so in order to get rid of it 1. Make the nesting lvl bigger or 2. Remove xdebug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.