'Parsing a HTML string

I have folling HTML string in a variable (simplified)

<div class="parent">
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
</div>

Now I have set a new structure like:

<div class="parent">
    <figure class="item"><img></figure>
    <div class="special">
        <figure class="item"><img></figure>
        <figure class="item"><img></figure>
    </div>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
</div>

How do I parse this with php? I have looked at DOMDocument but can't find a method to do this.

The problem is, with DOMDocument or with simpleHTML: I need to change the DOM structure. I do not a find a method to do this.

Example code (in $output is the first HTML string above):

$doc= new DOMDocument();
$doc->loadHTML($output);
    
$items=$doc->getElementsByTagName('figure');
foreach ($items as $item){
        // Now i am on the figure node, here I can do something like:
        // $doc->appendChild
        // or $doc->createAttribute
        // But I have to add a div above two nodes.
}

I need to "sum up" every second and third figure into a new div. But how I can do this with DOMDocument or simpleHTML? I would like to break the DOM structure into a new one. Maybe my approach with the DOM is wrong, and it can be solved differently?

php


Solution 1:[1]

I am using PHP Simple HTML Dom for parsing: https://simplehtmldom.sourceforge.io/

// Create a DOM object from a string
$html = str_get_html('<div class="parent">
    <figure class="item"><img></figure>
    <div class="special>
        <figure class="item"><img></figure>
        <figure class="item"><img></figure>
    </div>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
    <figure class="item"><img></figure>
</div>');

// Example -> is similar with jQuery accessing the DOM
echo $html->find("div.parent div.special figure img", 0);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valeriu Ciuca