@ 2018-04-28T19:50:30Z <?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());
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.3.0 - 8.3.15 , 8.4.1 - 8.4.2 Warning: Attempt to read property "parentNode" on null in /in/bajs1 on line 168
Fatal error: Uncaught Error: Call to a member function removeChild() on null in /in/bajs1:168
Stack trace:
#0 {main}
thrown in /in/bajs1 on line 168
Process exited with code 255 . Output for 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.31 , 8.2.0 - 8.2.27 List size: 0
object(DOMNodeList)#182 (1) {
["length"]=>
int(0)
}
string(12) "<div></div>
"
preferences:dark mode live preview
83.51 ms | 409 KiB | 5 Q