3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php header('Content-Type: text/plain; charset="utf-8"'); $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadXML(<<<__XML <?xml version="1.0" encoding="UTF-8"?> <root> <element>1</element> <element attr="a">2</element> <element>3</element> <element>4</element> <element attr="a">5</element> <element attr="a">6</element> <element>7</element> <element>8</element> </root> __XML ); $elements = $dom->getElementsByTagName('element'); $elements_array = iterator_to_array($elements); foreach ($elements_array as $element) { /** @var $element DomElement */ if ($element->hasAttribute('attr')) { $element->parentNode->removeChild($element); } } echo $dom->saveXML();
based on ieN3X
Output for 5.5.0 - 7.2.6
<?xml version="1.0" encoding="UTF-8"?> <root> <element>1</element> <element>3</element> <element>4</element> <element>7</element> <element>8</element> </root>