Menu

#42 Getting only the first level of nested tag

v1.0_(example)
closed
nested tags (1)
1
2019-04-20
2019-04-19
Gimmy
No

Hi,
I'm working with nested tags. I'd like to get the first level of tags only. How can I do?
For example...

$str = <<<HTML
<div>
    no children 1
</div>
<div>
    first parent
    <div>
        first child of first parent
        <div class="foo bar">second child of first parent</div>
    </div>
</div>
<div>
    no children 2
</div>
<div>
    second parent
    <div>
        first child of second parent
        <div class="foo bar">second child of second parent</div>
    </div>
</div>
HTML;

$html = str_get_html($str);

foreach ($html->find('div') as $onediv) {
    echo $onediv . '<hr />';
}

Returns the list of all the divs...

no children 1
first parent
first child of first parent
second child of first parent
first child of first parent
second child of first parent
second child of first parent
no children 2
second parent
first child of second parent
second child of second parent
first child of second parent
second child of second parent
second child of second parent

... but I'd like to get only the parents (first level tags).

no children 1
first parent (with nested content)
no children 2
second parent (with nested content)

This is also for nested tables.

Do you have suggestions?

Thank you

Giorgio

Discussion

  • LogMANOriginal

    LogMANOriginal - 2019-04-19

    Yes, this is pretty simple actually. The only requirement is that all elements must have a common ancestor. In your example, all divs are at the document root, which doesn't work. Put them inside a common element (body for example):

    $str = <<<HTML
    <body>
        <div>
            no children 1
        </div>
        <div>
            first parent
            <div>
                first child of first parent
                <div class="foo bar">second child of first parent</div>
            </div>
        </div>
        <div>
            no children 2
        </div>
        <div>
            second parent
            <div>
                first child of second parent
                <div class="foo bar">second child of second parent</div>
            </div>
        </div>
    </body>
    HTML;
    

    Now you can make use of the child combinator:

    foreach ($html->find('body > div') as $onediv) {
        echo $onediv . '<hr />' . PHP_EOL;
    }
    

    This will give you exactly what you want.

    You can combine it with other combinators to get more specific elements. Here is a complete list of supported selectors: https://simplehtmldom.sourceforge.io/docs/1.8/api/simple_html_dom_node/find/#supported-selectors

     
  • Gimmy

    Gimmy - 2019-04-19

    Aha, that's great!
    Thank you.
    Gimmy

     
  • LogMANOriginal

    LogMANOriginal - 2019-04-20
    • status: open --> closed
    • assigned_to: LogMANOriginal
     
  • LogMANOriginal

    LogMANOriginal - 2019-04-20

    You are welcome.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.