I have a complex HTML structure but a part of it looks like this
....
<div class="otherclass">
<div class="myclass">
<b>some HTML</b>
...
</div>
<div class="myclass">
<b>some HTML</b>
...
</div>
</div>
...
I would like to transform it into
...
<div class="otherclass">
<span>
<b>some HTML</b>
...
</span>
<span>
<b>some HTML</b>
...
</span>
</div>
...
What I currently have is
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*/div[@class='myclass']");
foreach( $elements as $element) {
$new = $doc->createElement('span', $element->nodeValue);
$parent = $element->parentNode;
$parent->removeChild($element);
$parent->appendChild($new);
}
But this will strip all HTML inside:
...
<div class="otherclass">
<span>some HTML...</span>
<span>some HTML...</span>
</div>
...
I have the HTML as string as well