$html = 'This is some content'; $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $content = $xpath->query('//div[@class="content"]/text()')->item(0)->nodeValue; echo $content;
$xml = 'This code loads an XML document into a DOMDocument object, creates a DOMXPath object, and uses it to select all item elements. It then loops through the matching elements, extracts their id and nodeValue attributes, and displays them. Package library: This library is a part of the PHP DOM extension, which is included in PHP by default.'; $doc = new DOMDocument(); $doc->loadXML($xml); $xpath = new DOMXPath($doc); $items = $xpath->query('//item'); foreach ($items as $item) { $id = $item->getAttribute('id'); $value = $item->nodeValue; echo "ID: $id, Value: $value\n"; } - Item 1
- Item 2