3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $XML = ' <html> <h1>Hello</h1> <ol> <li>test (no id)</li> <li xml:id="i2">test i2</li> </ol> </html> '; $doc = new DOMDocument; $doc->loadXML($XML); $xpath = new DOMXpath($doc); // get the ol $olNode = $xpath->evaluate('//ol[1]', NULL, FALSE)->item(0); // append a new list item $olNode->appendChild($liNode = $doc->createElement('li')); $liNode->appendChild($doc->createTextNode('test i3')); $liNode->setAttribute('xml:id', 'i3'); // show new DOM state print $doc->saveXML(); // insert a new list item before the others $olNode->insertBefore($liNode = $doc->createElement('li'), $olNode->firstChild); $liNode->appendChild($doc->createTextNode('test i0')); $liNode->setAttribute('xml:id', 'i0'); // show new DOM state print $doc->saveXML(); // fetch the li nodes without an xml:id $nodes = $xpath->evaluate('//ol/li[not(@xml:id)]', NULL, FALSE); // and remove them in reverse order for ($i = $nodes->length - 1; $i >= 0; --$i) { $nodes->item($i)->parentNode->removeChild($nodes->item($i)); } // show new DOM state print $doc->saveXML(); // replace the li[@xml:id'i2'] with a new node $liNode = $doc->getElementById('i2'); $followingNode = $liNode->nextSibling; $odNode = $liNode->parentNode; $olNode->removeChild($liNode); $olNode->insertBefore($liNode = $doc->createElement('li'), $followingNode); $liNode->appendChild($doc->createTextNode('test i2 replaced')); $liNode->setAttribute('xml:id', 'i2'); // show new DOM state print $doc->saveXML(); // NOT REFRESHING!?! var_dump($doc->getElementById('i2')); // object(DOMElement)#5 (0) { } li //CAN_NOT_doMORESomeChange($doc); $doc->loadXML($doc->saveXML()); // only way to refresh? print $doc->getElementById('i2')->tagName; //OK, is there // be ware that the xpath still uses the old document var_dump($xpath->document === $doc);
Output for 5.3.11 - 5.3.29, 5.4.1 - 5.6.21, 7.0.0 - 7.1.0
<?xml version="1.0"?> <html> <h1>Hello</h1> <ol> <li>test (no id)</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li>test (no id)</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li xml:id="i2">test i2 replaced</li> <li xml:id="i3">test i3</li></ol> </html> object(DOMElement)#8 (18) { ["tagName"]=> string(2) "li" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "li" ["nodeValue"]=> string(16) "test i2 replaced" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "li" ["baseURI"]=> string(1) "/" ["textContent"]=> string(16) "test i2 replaced" } libool(false)
Output for hhvm-3.10.0 - 3.12.0
<?xml version="1.0"?> <html> <h1>Hello</h1> <ol> <li>test (no id)</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li>test (no id)</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li xml:id="i2">test i2 replaced</li> <li xml:id="i3">test i3</li></ol> </html> object(DOMElement)#10 (18) { ["nodeName"]=> string(2) "li" ["nodeValue"]=> string(16) "test i2 replaced" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "li" ["baseURI"]=> string(1) "/" ["textContent"]=> string(16) "test i2 replaced" ["tagName"]=> string(2) "li" ["schemaTypeInfo"]=> NULL } libool(false)
Output for 5.3.3 - 5.3.10, 5.4.0
<?xml version="1.0"?> <html> <h1>Hello</h1> <ol> <li>test (no id)</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li>test (no id)</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li xml:id="i2">test i2</li> <li xml:id="i3">test i3</li></ol> </html> <?xml version="1.0"?> <html> <h1>Hello</h1> <ol><li xml:id="i0">test i0</li> <li xml:id="i2">test i2 replaced</li> <li xml:id="i3">test i3</li></ol> </html> object(DOMElement)#8 (0) { } libool(false)
Output for 5.2.6 - 5.3.2
Catchable fatal error: Argument 2 passed to DOMXPath::evaluate() must be an instance of DOMNode, null given in /in/nYu31 on line 18
Process exited with code 255.
Output for 5.2.3 - 5.2.5
Warning: DOMXPath::evaluate() expects at most 2 parameters, 3 given in /in/nYu31 on line 18 Fatal error: Call to a member function item() on a non-object in /in/nYu31 on line 18
Process exited with code 255.
Output for 5.1.0 - 5.2.2
<br /> <b>Warning</b>: DOMXPath::evaluate() expects at most 2 parameters, 3 given in <b>/in/nYu31</b> on line <b>18</b><br /> <br /> <b>Fatal error</b>: Call to a member function item() on a non-object in <b>/in/nYu31</b> on line <b>18</b><br />
Process exited with code 255.
Output for 5.0.0 - 5.0.5
<br /> <b>Fatal error</b>: Call to undefined method DOMXPath::evaluate() in <b>/in/nYu31</b> on line <b>18</b><br />
Process exited with code 255.
Output for 4.4.2 - 4.4.9
<br /> <b>Parse error</b>: syntax error, unexpected T_OBJECT_OPERATOR in <b>/in/nYu31</b> on line <b>18</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
<br /> <b>Parse error</b>: parse error, unexpected T_OBJECT_OPERATOR in <b>/in/nYu31</b> on line <b>18</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/nYu31</b> on line <b>18</b><br />
Process exited with code 255.