1

Html

<table>
<tr><td></td></tr>  //1st row

<tr><td></td></tr>  //2nd row

<tr><td></td></tr>  //3rd row

<tr><td></td></tr>  //4th row

<tr><td></td></tr>  //5th row
</table>

What I want to do

if (intval($rows) > 3) {

            delete all rows after 3rd row 
        }

I am using below php code to get html page

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

I hope my question clear enough to understand

Full code

<?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
        }
        ?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sukhjit dhot
  • 353
  • 2
  • 5
  • 18

4 Answers4

1

Here is a very simplistic, and untested, method :

//--- 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");

References: http://www.php.net/manual/en/domdocument.loadhtmlfile.php http://www.php.net/manual/en/domdocument.getelementsbytagname.php http://www.php.net/manual/en/domnode.removechild.php http://www.php.net/manual/en/domdocument.savehtmlfile.php

Hope this is of some help.

0

I would first extract the table using a regex such as \<table>.+<\/table>\, then strip the <table> </table> tags.

turn the string into array using exlode with <tr> as the delimiter and finally reconstruct the table using the first 3 items of the array

that is how i would attempt it, not sure it is applicable to your case. obviously you are scraping another site, so it depends a lot of how consistent the code is.

Georges Brisset
  • 246
  • 2
  • 6
0

jeff posted a good solution, so if you are interested in using any 3rd party libraries.
I suggest you to use ganon.php

<?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;
?>
bystwn22
  • 1,776
  • 1
  • 10
  • 9
0

Using jquery you can try as following

<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>
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89