English 中文(简体)
删除超文本表格最后一栏——使用序号
原标题:Removing the last column of an HTML table - using preg_replace

在简单的超文本表中,我想删除最后一栏。

<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和tdrowspan

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

问题:我如何去掉最后一栏,内容如何?

最佳回答

我猜想这样做。

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

假设每一行结尾处的批号(</tr>)与你的榜样不同。

<Edit: 这将消除任何<代码><td>或<th>、<条码>、<条码>、<条码>、>、>、>、>、<>、>、<>、>、<>、>、>、<>、>、>、>、>、<>、>、<>、>、>、>、>、>、<>、>、<>、>、>、<>、<>、>、<>、>、<>、>、<>、>、>、>、<>、>、>、>、>、>、>、>、>、>、>、>、>、>、>、>、>、>、[、>、>、>、

working example

问题回答
<?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();

http://codepad.org/c8wLrVl0”rel=“nofollow” See it working





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签