我有一個html表,我想將其列轉換爲行。列可以包含計劃文本,表單元素(輸入,選擇,複選框)或html標記(span,div)。考慮下面的html:如何將html表列轉換爲錶行,dom對象,php
<html>
<body>
<table>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
</table>
</body>
</html>
DOM操作之後,我需要以下條件:
<html>
<body>
<table>
<tr>
<td>Cell 1</td>
</tr>
<tr>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
</tr>
<tr>
<td>Cell 5</td>
</tr>
<tr>
<td>Cell 6</td>
</tr>
</table>
</body>
</html>
$rows = $xpath->query('xpath to table/tr');
foreach ($rows as $row){
$cols = $xpath->query('xpath to table/tr/*');
$row->parentNode->removeChild($row);
foreach ($cols as $col){
$newNode = $this->createElement('tr');
$newNode->appendChild($col);
$node->appendChild($newNode);
}
}
echo $dom->saveHTML();
其工作正常,但其加入空行:
<table>
<tr></tr>
<tr></tr>
<tr>
<td>Cell 1</td>
</tr>
<tr>
<td>Cell 2</td>
</tr>
<table>
任何想法我做錯了什麼?