V
V
Vladimir Mironov2014-09-25 01:38:40
PHP
Vladimir Mironov, 2014-09-25 01:38:40

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

3 answer(s)
P
Philipp, 2014-09-25
@zoonman

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.

S
Sergey Romanov, 2014-09-25
@Serhioromano

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);

S
SHAKIRA, 2014-09-25
@SHAKIRA

Give this work to JS. Don't torture the poor server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question