3v4l.org

run code in 300+ PHP versions simultaneously
<?php class BookXML extends \DOMDocument { public function filterByYear(int $year) { $books = []; $document = $this; $xpath = new DOMXPath($document); $booksObjs = $document->documentElement; $query = 'string(year)'; foreach ($booksObjs->childNodes as $booksObj) { $yearxml = $xpath->evaluate($query, $booksObj); if ($yearxml == $year) { $books[] = $booksObj; } } return $books; } } $content = ' <xml> <row> <year>2015</year> <month>september</month> </row> </xml> '; $xml = new BookXML(); $xml->loadXML($content); $filteredXML = $xml->filterByYear(2015); print_r($filteredXML);

preferences:
142.7 ms | 404 KiB | 5 Q