i want to warp an <img>
element in a <p style="text-align:center"></p>
.
this is the code i have so far:
$dom_err = libxml_use_internal_errors(true);
$dom = new domdocument('1.0', 'utf-8');
$dom->loadhtml(mb_convert_encoding($arr['nm_corpo_artigo'], 'html-entities', 'utf-8'));
$xpath = new domxpath($dom);
foreach ($xpath->query("//figure/img") as $img) {
$img->setattribute('style','max-width: 100%; height: auto;');
$img->setattribute('class','mb-4 mt-4');
}
$body = $dom->savehtml();
and this is the structure genrated by the code:
at the end iwould like to have:
<figure ...>
<p style="text-align:center"><img ...><figcaption>...</p>
</figure ...>
...
<?xml version="1.0" standalone="yes"?>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd">
<html>
<body>
<p>lorem ipsum dolor sit amet, consectetur adipiscing elit. mauris elementum sagittis est in vestibulum. maecenas vitae auctor nisi, id ullamcorper ex. ut l ?
<p>praesent et ante ac dolor sagittis ultricies ac ac ligula. quisque iaculis lorem non est ornare, ornare varius justo aliquet. vestibulum quam tortor, pos ?
<figure class="image">
<img src="/storage/12/articles/pictures/body_1574716135628_045090d10819d1777ed93e4e1a1cf079.jpeg"/>
<figcaption>ttttt</figcaption>
</figure>
<p>pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. aenean in urna nibh. quisque fermentum lobortis euismod. ut ?
<p>in pharetra efficitur sapien, a venenatis velit. donec rutrum ut nunc nec mollis. suspendisse ac auctor purus. curabitur nec eleifend ipsum, eu aliquet t ?
<p>vestibulum ullamcorper ante pharetra quam pharetra dictum. fusce sed turpis eget lacus pretium sollicitudin malesuada sagittis nulla. integer pulvinar or ?
</body>
</html>
what you need to do is create a new paragraph element (with the new attribute) and replace the image tag with this new tag and then add the img tag back into the new paragraph tag...