PHP DOM HTML 表格操作

标签 php html dom

我试图找出为什么这段代码不起作用。它从代码的另一部分获取 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/

相关文章:

php - mkdir() 上的权限被拒绝

PHP mysqli - 从准备好的语句返回关联数组

html - 不同屏幕尺寸 Bootstrap 导航栏中心对齐

javascript - 使用jquery在div中添加一个又一个动画

html - 为什么各大网站都没有通过 W3C 验证?

jQuery 仅选择子级直到特定类

php - 我如何发送 JobId 并在 View 中 append 值。 Ajax

php - 使用 UUID 将权限分配给 spatie/laravel-permission 中的角色时出现非法偏移类型

html - ShinyDashboard 中的 CSS Reactive ValueBoxes

jquery - 使用 jQuery 访问 Etherpad 上的内容 &lt;iframe&gt;