Works - //div[@class="remove-me"] or //div[@class="remove-me"]/text()
Not working - //div[@class="remove-me"]/@id
<?php
$content = '<div class="keep-me">Keep this div</div><div class="remove-me" id="test">Remove this div</div>';
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($content);
libxml_clear_errors();
$xPath = new DOMXpath($dom);
$domNodeList = $xPath->query('//div[@class="remove-me"]/@id');
$ids = []; // container of deleted elements
foreach ( $domNodeList as $domElement ) {
$ids[] = $domElement->nodeValue;
}
echo $ids[0];
?>