Php 从HTML表中删除x行之后的行

Php 从HTML表中删除x行之后的行,php,html-table,Php,Html Table,Html 我使用下面的php代码来获取html页面 我希望我的问题足够清楚,可以理解 完整代码 我将首先使用正则表达式(如\.+\)提取表,然后 去掉标签 使用exlode作为分隔符将字符串转换为数组 最后,使用数组的前3项重构表 这就是我尝试的方式,不确定它是否适用于你的情况。显然,您正在删除另一个站点,因此这在很大程度上取决于代码的一致性。这里有一个非常简单且未经测试的方法: //--- create a new DOM document $doc = new DOMDocument();

Html

我使用下面的php代码来获取html页面

我希望我的问题足够清楚,可以理解

完整代码


我将首先使用正则表达式(如\.+\)提取表,然后 去掉标签

使用exlode作为分隔符将字符串转换为数组 最后,使用数组的前3项重构表


这就是我尝试的方式,不确定它是否适用于你的情况。显然,您正在删除另一个站点,因此这在很大程度上取决于代码的一致性。

这里有一个非常简单且未经测试的方法:

//--- create a new DOM document
$doc = new DOMDocument();
//--- load your file
$doc->loadHTMLFile("filename.html");
//--- point to the tables [0] means first table in the file
$tables = $doc->getElementsByTagName('table')[0];

//--- get all the tr within the specified table
$tr = $tables->getElementsByTagName('tr');
//--- loop backwards
for( $x=count($tr)-1; $x>2 $x-- ) {
  //--- remove the node (not sure which one will work)
  $old = $tr->removeChild($tr[$x]);
  $old = $tr->removeChild( $tr->item($x) );
}
//--- save the new file
$doc->saveHTMLFile("/tmp/test.html");
参考资料:

希望这对您有所帮助。

jeff发布了一个很好的解决方案,如果您对使用任何第三方库感兴趣的话。 我建议你使用


使用jquery,您可以尝试以下操作

<script src='http://code.jquery.com/jquery-latest.min.js' type="text/javascript" ></script>

<?php
$html = '<table id="mytable">
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
    <tr><td>4</td></tr>
    <tr><td>5</td></tr>
  </table>';

echo $html;

?>

<script>
$(function() {
    var TRs = $("#mytable tr");
    for(i=0; i<TRs.length; i++) {
        if(i>=3) {
        $(TRs[i]).remove(); 
       }
   }
});
</script>

+我应该先等待评论,然后再努力…你能展示一下将行打印到屏幕上的代码吗?这只是一个html表,还是你从数据库检索数据后用php打印的?这只是一个html表…我已经编辑了我的主要帖子,你可以看到完整的代码答案被编辑了,现在就试试吧。这对我来说很有效
$index = substr_count(strtolower(file_get_contents('index.html')), '<tr>');
<?php
        $htaccess = file_get_contents('index.html');
        $new_htaccess = str_replace('<table><tr><td>first row data</td></tr>', '<table><tr><td>first row data</td></tr><tr><td>sec row data</td></tr>', $htaccess);
        $pos = strpos($htaccess, $ssa);
        if ($pos == false) {
            file_put_contents('index.html', $new_htaccess);
        } else {

        }

        $index = substr_count(strtolower(file_get_contents('index.html')), '<tr>');

        if (intval($index) > 20) {
            //delete end rows and add a new one
        }
        ?>
//--- create a new DOM document
$doc = new DOMDocument();
//--- load your file
$doc->loadHTMLFile("filename.html");
//--- point to the tables [0] means first table in the file
$tables = $doc->getElementsByTagName('table')[0];

//--- get all the tr within the specified table
$tr = $tables->getElementsByTagName('tr');
//--- loop backwards
for( $x=count($tr)-1; $x>2 $x-- ) {
  //--- remove the node (not sure which one will work)
  $old = $tr->removeChild($tr[$x]);
  $old = $tr->removeChild( $tr->item($x) );
}
//--- save the new file
$doc->saveHTMLFile("/tmp/test.html");
<?php
  require_once( "ganon.php" );
  // Your html
  $html = '<table>
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
    <tr><td>4</td></tr>
    <tr><td>5</td></tr>
  </table>';
  // load the html
  $html = str_get_dom( $html );
  // search for our table
  if ( $table = $html( "table", 0 ) ) {
    // get all rows which is after 3rd row, here 0 is 1, so 3rd row is 2
    if ( $rows = $html( "tr:gt(2)" ) ) {
      // loop through rows
      foreach( $rows as $row ) {
        // .... and delete them
        $row->delete();
      }
    }
  }
  // output your modified html
  echo $html;
?>
<script src='http://code.jquery.com/jquery-latest.min.js' type="text/javascript" ></script>

<?php
$html = '<table id="mytable">
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
    <tr><td>4</td></tr>
    <tr><td>5</td></tr>
  </table>';

echo $html;

?>

<script>
$(function() {
    var TRs = $("#mytable tr");
    for(i=0; i<TRs.length; i++) {
        if(i>=3) {
        $(TRs[i]).remove(); 
       }
   }
});
</script>