使用PHP和DOMDocument替换<img>标记

I'm using PHP and I want to delete the <p>tags from this code:

<p><img alt="" src="/sites/default/files/art/w2.jpg" style="height:395px; width:800px" /></p>

I then want to rewrite the img tag like this:

<figure><img src="/sites/default/files/art/w2.jpg" /></figure>

I'm trying to achieve this using DOMDocument.

This is my code:

        $document = new DOMDocument;
        $document->loadHTML($body);
        $embeds= $document->getElementsByTagName('img');
        foreach ($embeds as $embed) {
            $src= $embed->getAttribute('src');
            $link= $document->createElement('figure');
            $link= $document->createElement('img');
            $link->setAttribute('src', $src);
            $embed->parentNode->replaceChild($link, $embed);
        }

So far I have not been able to do this.

I tried the following and it seems to produce the desired result. I have found that iterating backwards through a collection of dom nodes when deleting / modifying often works when other methods fail.

        $body='
        <html>
            <head>
                <title>DOM</title>
            </head>
            <body>
                <p>
                    <img alt="" src="/sites/default/files/art/w2.jpg" style="height:395px; width:800px" />
                </p>
                <p>
                    <img alt="" src="/sites/default/files/art/w3.jpg" style="height:395px; width:800px" />
                </p>
                <p>
                    <img alt="" src="/sites/default/files/art/w4.jpg" style="height:395px; width:800px" />
                </p>
            </body>
        </html>';


        $dom = new DOMDocument;
        $dom->loadHTML( $body );

        $col = $dom->getElementsByTagName('img');
        if( !empty( $col ) ){

            for ( $i = $col->length; --$i >= 0; ) {
                $node = $col->item( $i );
                if( $node ){
                    $src = $node->getAttribute('src');
                    if( $src ){

                        $img = $dom->createElement('img');
                        $attrib = $dom->createAttribute( 'src' );
                        $attrib->nodeValue=$src;
                        $img->appendChild( $attrib );

                        $fig = $dom->createElement('figure');
                        $fig->appendChild( $img );

                        $node->parentNode->parentNode->replaceChild( $fig, $node->parentNode );
                    }
                }
            }
        }
        echo '<textarea cols=100 rows=10>',$dom->saveHTML(),'<textarea>';

Пример для Laravel 5.7 с сохранением картинки по $path и замены на него в поле text (содержание статьи с картинками base64) запроса

Example 5.7 with Laravel you save the image at $path and replace it in the text field (the contents of the article with the images base64 encoded) query

$change=$request->row;
    $change['text']='';
    $id=DB::table($request->name)->insertGetId($change);

    $dom = new DOMDocument('1.0');
    $dom->loadHTML($request->row['text']);
    $i=0;
    foreach ($dom->getElementsByTagName('img') as $img) {
        $src= $img->getAttribute('src');
        $image_cont = explode(",", $src);
        $path='tables/'.$request->name.'/'.$id.'/'.$i.'.png';
        Storage::disk('MyDiskDriver')->put($path, base64_decode($image_cont[1]));
        $i=$i+1;
    }
    $new=$dom->saveHTML();
    $text=preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\s*~i', '', $new);
    DB::table($request->name)->where('id', '=', $id)->update(['text' =>  $test]);