This is mentioned in a couple of comments on the DomNode::removeChild
documentation, with the issue apparently being how the iterator pointer on the foreach not being able to deal with the fact that you are removing items from a parent array while looping through the list of children (or something).
The recommended fix is to loop through the main node first and push the child nodes you want to delete to its own array, then loop through that "to-be-deleted" array and deleting those children from their parent. Example:
$dom = new DOMDocument();
@$dom->loadHTML($description);
$pTag = $dom->getElementsByTagName('p');
$spotid_children = array();
foreach ($pTag as $value) {
/** @var DOMElement $value */
$id = $value->getAttribute('data-spotid');
if ($id) {
$spotid_children[] = $value;
}
}
foreach ($spotid_children as $spotid_child) {
$spotid_child->parentNode->removeChild($spotid_child);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…