Answer the question
In order to leave comments, you need to log in
How to change tag attributes on output?
Good day to all. There is a blog. Content is retrieved from the database. The content contains pictures and some user made a lazy-load
<img class="main-image lazy-load img-responsive" src="/img/loading.svg" alt="ТОП 10: Рейтинг лучших вытяжных вентиляторов" data-src="/uploads/blog/main/thumb/top_950_500.jpg">
function replaceImageDataSrc($content)
{
$dom = new DOMDocument();
$dom->loadHTML($content);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$dataSrc = $image->getAttribute('data-src');
$src = $image->getAttribute('src');
$image->setAttribute('src', $dataSrc);
$image->setAttribute('data-src', $src);
}
return $dom->saveHTML();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question