3

I am using Windows software to organize a tourpool. This program creates (among other things) HTML pages with rankings of participants. But these HTML pages are quite hideous, so I am building a site around it.

To show the top 10 ranking I need to select the first 10 out of about 1000 participants of the generated HTML file and put it on my own site.

To do this, I used:

// get top 10 ranks of p_rank.html
$file_contents = file_get_contents('p_rnk.htm');
$start = strpos($file_contents, '<tr class="header">'); 

// get end  
$i = 11;
while (strpos($file_contents, '<tr><td class="position">'. $i .'</td>', $start) === false){
   $i++;
}

$end = strpos($file_contents, '<td class="position">'. $i .'</td>', $start);

$code = substr($file_contents, $start, $end); 
echo $code;

This way I get it to work, only the last 3 columns (previous position, up or down and details) are useless information. So I want these columns deleted or find a way to only select and display the first 4.

How do i manage this?


EDIT

I adjusted my code and at the end I only echo the adjusted table.

<?php

$DOM = new DOMDocument;
$DOM->loadHTMLFile("p_rnk.htm");

$table = $DOM->getElementsByTagName('table')->item(0);
$rows = $table->getElementsByTagName('tr');

$cut_rows_after = 10;
$cut_colomns_after = 3;

$row_index = $rows->length-1;

while($row = $rows->item($row_index)) {
    if($row_index+1 > $cut_rows_after)
        $table->removeChild($row);
    else {
        $tds = $row->getElementsByTagName('td');
        $colomn_index = $tds->length-1;
        while($td = $tds->item($colomn_index)) {
            if($colomn_index+1 > $cut_colomns_after)
                $row->removeChild($td);
            $colomn_index--;
        }
    }
    $row_index--;
}

echo $DOM->saveHTML($table);

?>
19
  • 1
    Welcome to StackOverflow. Please specify what software do you use; clarify what top10 means here; provide some code that you have tried so that we can see more clearly what the problem is. Commented Jun 13, 2016 at 15:41
  • 1
    Thanks YakovL. I adjusted my post. Do you need more information? Commented Jun 13, 2016 at 16:40
  • Yes, great, now the question is quite clear. Commented Jun 13, 2016 at 23:20
  • YakovL, I've updated my post. Can you point me in the right direction? Commented Jun 15, 2016 at 7:50
  • Actually, you're close. You may either first remove all unnecessary data from the table (now, it seems, you remove only some tds) and than print it or print it cell-wise (get a tr element, get all the td elements from it, print those which are needed; remember to add, <table>, <\table>, <tr> and </tr> tags in this case). Commented Jun 15, 2016 at 16:00

1 Answer 1

0

I'd say that the best way to deal with such stuff is to parse the html document (see, for instance, the first anwser here) and then manipulate the object that describes DOM. This way, you can easily extract the table itself using various selectors, get your 10 first records in a simpler manner and also will be able to remove unnecessary child (td) nodes from each line (using removeChild). When you're done with modifying, dump the resulting HTML using saveHTML.

Update:

ok, here's a tested code. I removed the necessity to hardcode the numbers of colomns and rows and separated the desired numbers of colomns and rows into a couple of variables (so that you can adjust them if neede). Give the code a closer look: you'll notice some details which were missing in you code (index is 0..999, not 1..1000, that's why all those -1s and +1s appear; it's better to decrease the index instead of increasing because in this case you don't have to case about numeration shifts on removing; I've also used while instead of for not to care about cases of $rows->item($row_index) == null separately):

<?php
    $DOM = new DOMDocument;
    $DOM->loadHTMLFile("./table.html");

    $table = $DOM->getElementsByTagName('tbody')->item(0);
    $rows = $table->getElementsByTagName('tr');

    $cut_rows_after = 10;
    $cut_colomns_after = 4;

    $row_index = $rows->length-1;
    while($row = $rows->item($row_index)) {
        if($row_index+1 > $cut_rows_after)
            $table->removeChild($row);
        else {
            $tds = $row->getElementsByTagName('td');
            $colomn_index = $tds->length-1;
            while($td = $tds->item($colomn_index)) {
                if($colomn_index+1 > $cut_colomns_after)
                    $row->removeChild($td);
                $colomn_index--;
            }
        }
        $row_index--;
    }

    echo $DOM->saveHTML();
?>

Update 2:

If the page doesn't contain tbody, use the container which is present. For instance, if tr elements are inside a table element, use $DOM->getElementsByTagName('table') instead of $DOM->getElementsByTagName('tbody').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.