删除HTML表的最后一列 - 使用preg_replace

时间:2012-05-17 11:45:00

标签: php dom preg-replace

在一个简单的HTML表格中我想删除最后一列

<table>
<tbody>
<tr>
    <th>1</th>
    <th>2</th>
    <th>3</th>
    <th rowspan="3">I want to remove this</th>
</tr>

<tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td rowspan="3">I want to remove this</td>
</tr>

我正在使用此代码,但我仍然留下了内容和 th 和td rowspan

$myTable = preg_replace('#</?td rowspan[^>]*>#i', '', $myTable);
echo $myTable

问题:如何删除最后一列及其内容?

2 个答案:

答案 0 :(得分:0)

我想这会做到这一点

preg_replace("/<(?:td|th)[^>]*>.*?<\/(?:td|th)>\s+<\/tr>/i", "</tr>", $myTable);

假设您在每行末尾都有结束tr标记(</tr>),这与您的示例不同

修改:这将删除所有<td><th>元素,然后关闭</tr>,无论它们是否具有任何属性

working example

答案 1 :(得分:0)

<?php

  // Create a new DOMDocument and load the HTML
  $dom = new DOMDocument('1.0');
  $dom->loadHTML($html);

  // Create a new XPath query
  $xpath = new DOMXPath($dom);

  // Find all elements with a rowspan attribute
  $result = $xpath->query('//*[@rowspan]');

  // Loop the results and remove them from the DOM
  foreach ($result as $cell) {
    $cell->parentNode->removeChild($cell);
  }

  // Save back to a string
  $newhtml = $dom->saveHTML();

See it working