Есть ли способ заменить тег img тегом div, как показано ниже?
Оригинальный html:
<div class="article">
<img src="/images/img-1.jpg" alt="alt for image">
</div>
Заменено html:
<div class="article">
<div style="background: transparent url(/images/img-1.jpg) no-repeat;
background-position: center center; background-size: cover; width: 566px;
height: 576px;">alt for image</div>
</div>
(Необязательно) Также можно использовать ширину и высоту из родительского div i.e. класса статьи в моем примере вместо определения фиксированного width: 566px; height: 576px;
?
Если возможно, я хочу использовать функцию str_replace
.
str_replace('?????', '?????', $article);
Edit:
В статье класса может быть несколько элементов, и в классе класса div могут быть другие элементы, из которых мне нужно изменить img на div.
Edit2:
Я имел в виду, что у меня может быть любой контент внутри статьи div и просто хочу заменить img на div.
У меня может быть:
<div class="article">
<h1>heading</h1>
<p>paragraph</p>
<img src="/images/img-1.jpg" alt="alt for image">
</div>
Или у меня может быть:
<div class="article">
<h3>heading</h3>
<p><img src="/images/img-1.jpg" alt="alt for image"> some paragraph </p>
</div>
Итак, у меня может быть что-нибудь внутри .article
div и из которого я хотел бы заменить img на div как
From:
<img src="/images/img-1.jpg" alt="alt for image" here-may-be-another-attribute-too>
To:
<div style="background: transparent url(/images/img-1.jpg) no-repeat;">alt for image</div>