I'm trying to use the DOMDocument in PHP to add/parse things in an HTML document. From what I could read, setting the formOutput to true and preserveWhiteSpace to false should keep the tabs and newlines in order, but it doesn't seem like it is for newly created or appended nodes.
Here's the code:
$dom = new \DOMDocument;
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->loadHTMLFile($htmlsource);
$tables = $dom->getElementsByTagName('table');
foreach($tables as $table)
{
$table->setAttribute('class', 'tborder');
$div = $dom->createElement('div');
$div->setAttribute('class', 'm2x');
$table->parentNode->insertBefore($div, $table);
$div->appendChild($table);
}
$dom->saveHTMLFile($html)
Here's what the HTML looks like:
<table>
<tr>
<td></td>
</tr>
</table>
Here's what I want:
<div class="m2x">
<table class="tborder">
<tr>
<td></td>
</tr>
</table>
</div>
Here's what I get:
<div class="m2x"><table class="tborder"><tr>
<td></td>
</tr></table></div>
Is there something I'm doing wrong? I've tried googling this as many different ways as I could thing of with no luck.