gpt4 book ai didi

PHP DOM HTML 表格操作

转载 作者:行者123 更新时间:2023-11-27 23:23:07 25 4
gpt4 key购买 nike

我想弄清楚为什么这段代码不起作用。它从代码的另一部分获取 HTML,从中取出表格,创建另一列并尝试将 td 元素移动到新创建列中的上方行。

导入的表:

+-------------------+-------------------+-------------------+
| Existing column1 | Existing column2 | Existing column3 |
+-------------------+-------------------+-------------------+
| A | B | C |
| D | E | F |
| G |
+-------------------+-------------------+-------------------+

我想尝试让它看起来像这样:

+-------------------+-------------------+-------------------+-------------+
| Existing column1 | Existing column2 | Existing column3 | New column1 |
+-------------------+-------------------+-------------------+-------------+
| A | B | C | |
| D | E | F | G |
+-------------------+-------------------+-------------------+-------------+

因此,每当 td 元素具有 text-info 类时,它会将其向上移动 tr 并将其附加到最后一个 评论

到目前为止我的代码:

$dom = new DOMDocument();//Loads DOM document
$dom->loadHTML($str);//Loads HTML from a previously set variable
$xpath = new DOMXPath($dom);
$tables = $xpath->query('//table[@class="behaviourtable table"]');//Get only table from HTML
$commsTable = '';
foreach ($tables as $table) {
$commsTable .= $dom->saveXML($table);
}

$commsHTML = new DOMDocument();
$commsHTML->loadHTML($commsTable);

$tr = $commsHTML->getElementsByTagName('tr');


$th = $commsHTML->createElement('th', 'Comment');
$tr->item(0)->appendChild($th);

$xpathcomms = new DOMXPath($commsHTML);
$comments = $xpathcomms->query('//td[@class="text-info"]');
if($comments->length > 0){
echo "if running";
foreach($comments as $comment){
$parent = $comment->parentNode;
$parent->appendChild($comment);
$commsHTML->saveXML($parent);
}

}


echo $commsHTML->saveHTML();

最佳答案

在您的代码中,您将 td 附加到它的原始父节点(什么都不做),而您实际上想做的是获取父节点 (tr),转到它的前一个兄弟(prev tr)并将 td 附加到那个 tr:

foreach($comments as $comment){
$parent = $comment->parentNode;
$parent->previousSibling->appendChild($comment);
}

这是一个完整的工作示例:

$str = <<<END
<table class="behaviourtable table">
<tr>
<th>Existing column1</th>
<th>Existing column2</th>
<th>Existing column3</th>
</tr><tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr><tr>
<td>D</td>
<td>E</td>
<td>F</td>
</tr><tr>
<td class="text-info">G</td>
</tr>
</table>
END;

$dom = new DOMDocument();//Loads DOM document
$dom->loadHTML($str);//Loads HTML from a previously set variable
$xpath = new DOMXPath($dom);
$tables = $xpath->query('//table[@class="behaviourtable table"]');//Get only table from HTML
$commsTable = '';
foreach ($tables as $table) {
$commsTable .= $dom->saveXML($table);
}

$commsHTML = new DOMDocument();
$commsHTML->loadHTML($commsTable);

$tr = $commsHTML->getElementsByTagName('tr');


$th = $commsHTML->createElement('th', 'Comment');
$tr->item(0)->appendChild($th);

$xpathcomms = new DOMXPath($commsHTML);
$comments = $xpathcomms->query('//td[@class="text-info"]');
if($comments->length > 0){
foreach($comments as $comment){
$parent = $comment->parentNode;
$parent->previousSibling->appendChild($comment);
}
}

echo $commsHTML->saveHTML();

检查这个工作示例:
https://3v4l.org/FZkNE

关于PHP DOM HTML 表格操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com