<?php
$data = <<<EOF
<table class="track_list track_table" id="track_table">
<tbody>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:visible"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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's Been A While</a> 05:49</div>
</td>
<td>
<div class="info_link" style="visibility:hidden"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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"> </div>
</td>
<td>
<div class="dl_link" style="visibility:hidden"> </div>
</td>
</tr>
<tr>
<td>
<div class="play_status"> </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> </td>
<td> </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());
?>
preferences:
24.65 ms | 406 KiB | 5 Q