繁体   English   中英

PHP DOM HTML 表格操作

[英]PHP DOM HTML table manipulation

我想弄清楚为什么这段代码不起作用。 它从代码的另一部分获取 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并将其附加到最后一个Comment

到目前为止我的代码:

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM