3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $data = <<<EOF <table class="track_list track_table" id="track_table"> <tbody> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">1.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/i-feel">I Feel</a> 04:18</div> </td> <td> <div class="info_link" style="visibility:visible">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:visible">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">2.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/wait">Wait</a> 06:03</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">3.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/superwoman">Superwoman</a> 03:46</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">4.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/in-the-night">In The Night</a> 05:41</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">5.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/what-to-do">What To Do</a> 03:52</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">6.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/its-been-a-while">It&#39;s Been A While</a> 05:49</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">7.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/move-on">Move On</a> 04:28</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">8.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/loverboy">Loverboy</a> 08:00</div> </td> <td> <div class="info_link" style="visibility:hidden">&nbsp;</div> </td> <td> <div class="dl_link" style="visibility:hidden">&nbsp;</div> </td> </tr> <tr> <td> <div class="play_status">&nbsp;</div> </td> <td> <div class="secondaryText track_number">9.</div> </td> <td> <div class="title"><a href="https://zars.bandcamp.com/track/parallel-world">Parallel World</a> 07:38</div> </td> <td>&nbsp;</td> <td>&nbsp;</td> </tr> </tbody> </table> EOF; $data = str_replace(["\r", "\n", "\t"], "", $data); $doc = new DOMDocument(); $doc->loadHTML('<div>' . $data . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new \DOMXpath($doc); // Remove all empty nodes $emptyNodes = $xpath->query("//text()[translate(normalize-space(.), '\"\xC2\xA0\', '') = '']"); foreach ($emptyNodes as $i => $node) { $node->parentNode->parentNode->removeChild($node->parentNode); } $list = $xpath->query(".//text()[translate(normalize-space(.), '\"\xC2\xA0\', '') != '']"); print_r("List size: $list->length \r\n"); function flattenDOMNodes(\DOMDocument &$doc, \DOMNodeLIst $list) { foreach ($list as $i => $node) { print_r("$i\r\n"); $parent = $node->parentNode; // Do not manipulate nodes directly under the root node to separate between tracks. if ($parent->parentNode !== $node->ownerDocument->documentElement) { if ($parent->previousSibling instanceof DOMText) { print_r("Also appending happens!\r\n"); $parent->previousSibling->appendData($node->nodeValue); $parent->parentNode->removeChild($parent); } elseif ($parent->nextSibling instanceof DOMText) { print_r("Also inserting happens!\r\n"); $parent->nextSibling->insertData(0, " $node->nodeValue "); $parent->parentNode->removeChild($parent); } else { $newNode = $doc->createTextNode($node->nodeValue); // Go up one level while ($parent->parentNode->childNodes->length === 1) { print_r("Up, up, up!\r\n"); $parent = $parent->parentNode; } print_r("This happens\r\n"); $parent->replaceChild($newNode, $parent->childNodes->item(0)); } } $xpath = new \DOMXpath($doc); $list = $xpath->query(".//text()[translate(normalize-space(.), '\"\xC2\xA0\', '') != '']"); } return $list; } var_dump(flattenDOMNodes($doc, $list)); var_dump($doc->saveHTML()); ?>
Output for 5.6.0 - 5.6.30, 7.1.0 - 7.2.6
List size: 0 object(DOMNodeList)#182 (1) { ["length"]=> int(0) } string(12) "<div></div> "
Output for hhvm-3.22.0
Warning: Invalid State Error in /in/bajs1 on line -1 Notice: Cannot access property on non-object in /in/bajs1 on line -1 Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function removeChild() on a non-object (null)' in /in/bajs1:-1 Stack trace: #0 {main}
Process exited with code 255.
Output for hhvm-3.18.5 - 3.21.3
Warning: Invalid State Error in /in/bajs1 on line 168 Notice: Cannot access property on non-object in /in/bajs1 on line 168 Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function removeChild() on a non-object (null)' in /in/bajs1:168 Stack trace: #0 {main}
Process exited with code 255.