Answer the question
In order to leave comments, you need to log in
How to use php to find all images in text and assign new attributes to them?
Actually, there is a publication (all in the database), which consists of text and pictures.
The task is to find images and assign them certain attributes.
How can this be implemented?
Answer the question
In order to leave comments, you need to log in
Retrieve a post from the database.
Create a DomDocument from it, recursively bypass childNodes, checking the node type, if it's an image, then setAttribute, render back to html. Save the document.
Theoretically, you can still try to fasten xpath, but I'm not very sure about referential integrity.
If the text is stored in HTML, then it's very simple
Use regular expressions.
$text = preg_replace('/<img(.*)(class="([a-z ]*)")([^>]*)>/isU', '<img \1 class="\3 myclass" \4>', $text);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question