3v4l.org

run code in 300+ PHP versions simultaneously
<?php function do_it($in) { $a = new DOMDocument(); $a->loadHTML($in); $xpath = new DOMXpath($a); foreach($xpath->query('/html/body//ul') as $node) _handle_uls($node); return $a->textContent; } function _handle_uls(DOMNode $node) { foreach (iterator_to_array($node->childNodes) as $subnode) { $textNode = $subnode->ownerDocument->createTextNode('* ' . trim($subnode->textContent) . "\n"); $subnode->ownerDocument->importNode($textNode); $subnode->parentNode->replaceChild($textNode, $subnode); } } $HTML = '<ul><li>Potatoes</li><li>Tomatoes</li><li>Carrots: <ul><li>Green carrots</li><li>Yellow carrots</li></ul></li></ul>'; echo do_it($HTML);
Output for 8.3.0 - 8.3.6
* Potatoes * Tomatoes * Carrots: Green carrotsYellow carrots
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Fatal error: Uncaught Error: Couldn't fetch DOMElement. Node no longer exists in /in/8aoOo:17 Stack trace: #0 /in/8aoOo(10): _handle_uls(Object(DOMElement)) #1 /in/8aoOo(26): do_it('<ul><li>Potatoe...') #2 {main} thrown in /in/8aoOo on line 17
Process exited with code 255.
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Warning: Couldn't fetch DOMElement. Node no longer exists in /in/8aoOo on line 17 Notice: Undefined property: DOMElement::$childNodes in /in/8aoOo on line 17 Fatal error: Uncaught TypeError: Argument 1 passed to iterator_to_array() must implement interface Traversable, null given in /in/8aoOo:17 Stack trace: #0 /in/8aoOo(17): iterator_to_array(NULL) #1 /in/8aoOo(10): _handle_uls(Object(DOMElement)) #2 /in/8aoOo(26): do_it('<ul><li>Potatoe...') #3 {main} thrown in /in/8aoOo on line 17
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Warning: Couldn't fetch DOMElement. Node no longer exists in /in/8aoOo on line 17 Fatal error: Uncaught TypeError: Argument 1 passed to iterator_to_array() must implement interface Traversable, null given in /in/8aoOo:17 Stack trace: #0 /in/8aoOo(17): iterator_to_array(NULL) #1 /in/8aoOo(10): _handle_uls(Object(DOMElement)) #2 /in/8aoOo(26): do_it('<ul><li>Potatoe...') #3 {main} thrown in /in/8aoOo on line 17
Process exited with code 255.

preferences:
177.44 ms | 402 KiB | 211 Q