G
G
GamiDPC2018-12-17 22:51:15
PHP
GamiDPC, 2018-12-17 22:51:15

Why is html attribute not showing through php xpath?

Why is the html attribute not showing through php?
https://wtools.io/php-sandbox/xF

Works - //div[@class="remove-me"] or //div[@class="remove-me"]/text()
Not working - //div[@class="remove-me"]/@id

Maybe there is an easier way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GamiDPC, 2018-12-17
@GamiDPC

Solved helped in another site

<?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];
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question