<?php $content = '<figure class="image image-style-align-left"><img src="https://placekitten.com/g/200/300"></figure><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>'; $dom = new DOMDocument(); libxml_use_internal_errors(true); // this needs to be encoded otherwise special characters get messed up. $domPart = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); $dom->loadHTML($domPart, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $domFigures = $dom->getElementsByTagName('figure'); foreach ($domFigures as $domFigure) { $img = $domFigure->getElementsByTagName('img')[0]; if ($img) { $img->setAttribute('src', "https://placekitten.com/g/400/500"); } } $result = $dom->saveHTML(); print_r($result);
You have javascript disabled. You will not be able to edit any code.