HTML テーブルがあり、その列を行に変換したいと考えています。列には、プラン テキスト、フォーム要素 (input、select、checkbox)、または html タグ (span、div) を含めることができます。次の html を検討してください。
<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>
私が間違っていることは何ですか??