I am trying to delete p
tags with data-spotid
attribute
$dom = new DOMDocument();
@$dom->loadHTML($description);
Like I commented, the easy solution would be to just cast the iterator to an array. E.g.:
$elements = iterator_to_array($elements);
But, if we're talking about performance, a better way would be to simply select only the required nodes. Neat side-effect, the removal-problem also goes away.
E.g.:
loadXML(<<<__XML
1
2
3
4
5
6
7
8
__XML
);
$xpath = new DOMXPath($doc);
$elements = $xpath->query('//element[@attr]');
foreach ($elements as $element) {
$element->parentNode->removeChild($element);
}
echo $doc->saveXML();
Demo: https://3v4l.org/CM9Fv