如果在 table 单元格中满足条件,如何删除 table 行? - preg_replace 或 domdocument 欢迎

How to remove table row if condition met in table cell? - preg_replace or domdocument are welcome

我的HTMLtable:

$table = <<< EOD
<table>
    <tbody>
        <tr style="border:none;">
            <td>1</td>
            <td>[[name_21]]</td>
            <td>[[charge_2]]</td>
        </tr>
        <tr style="border:1px solid #aaa;">
            <td>2</td>
            <td>[[name_13]]</td>
            <td>[[charge_5]]</td>
        </tr>
        <tr style="border:1px solid #ccc;">
            <td>3</td>
            <td>[[name_24]]</td>
            <td>[[charge_13]]</td>
        </tr>
        <tr style="border:1px solid #555;">
            <td>4</td>
            <td>[[name_22]]</td>
            <td>[[charge_22]]</td>
        </tr>
    </tbody>
</table>
EOD;

这就是我获取所有 charge_X 值的方式。

preg_match_all('/\[\[charge_(\d+)\]\]/', $table, $charge_ids);

删除单元格中包含 charge_13 的 table 行。

if (isset($charge_ids)) {
    if (array_key_exists('1', $charge_ids)) {
        $charge_ids = $charge_ids[1];
        $table_replaced = $table;

        foreach ($charge_ids as $charge_id) {
            if ($charge_id == '13') {
                // this line is removing table row if condition met in table cell.
                $table_replaced = preg_replace('/<tr[^>]*>(\[\[charge_'.$charge_id.'\]\])<\/tr>/iUums', 'a', $table_replaced);
            }
        }

        echo $table_replaced;
    }
}

此代码仍然无效。如果 table 单元格中有 charge_13,如何删除 table 行?

使用 DOMDocument。

$table = <<< EOD
<table>
    <tbody>
        <tr style="border:none;">
            <td>1</td>
            <td>[[name_21]]</td>
            <td>[[charge_2]]</td>
        </tr>
        <tr style="border:1px solid #aaa;">
            <td>2</td>
            <td>[[name_13]]</td>
            <td>[[charge_5]]</td>
        </tr>
        <tr style="border:1px solid #ccc;">
            <td>3</td>
            <td>[[name_24]]</td>
            <td>[[charge_13]]</td>
        </tr>
        <tr style="border:1px solid #555;">
            <td>4</td>
            <td>[[name_22]]</td>
            <td>[[charge_22]]</td>
        </tr>
    </tbody>
</table>
EOD;

$dom = new \DOMDocument();
$dom->loadHTML($table);
$xpath = new \DOMXPath($dom);

// loop all <tr> element.
foreach ($xpath->query('//tr') as $tr) {
    $tds = $tr->getElementsByTagName('td');
    // get total <td> in this <tr>
    $total_item = $tds->length;

    // loop to all <td> and check.
    for ($i = 0; $i <= ($total_item-1); $i++) {
        // get table cell value.
        $table_cell_value = $tds->item($i)->nodeValue;
        preg_match('/\[\[charge_(\d+)\]\]/', $table_cell_value, $charge_id);

        // check if condition is met, the charge_id is 13.
        if (is_array($charge_id) && array_key_exists(1, $charge_id) && $charge_id[1] == 13) {
            // condition met. charge_id is 13. remove this table row.
            $tr->parentNode->removeChild($tr);
        }
        unset($charge_id, $table_cell_value);
    }

    unset($tds, $total_item);
}

// get the result
$saved_html = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());
echo $saved_html;