Search code examples
phphtmlsimple-html-dom

php simple_html_dom select correct table data


I have table in html file (fragment what interesting me):

<h3 class="subTitle">Odbitki</h3>
<div class="subtitleBottomEdge"></div>
<div class="pad10">
  <table class="mainContentArea">
    <tr>
      <td class="labelFont">Wszystkie odbitki:</td>
      <td class="itemFont">49946</td>
    </tr>
    <tr>
      <td class="labelFont">Kompletne odbitki równoważności (A4/Letter):</td>
      <td class="itemFont">49945.4</td>
    </tr>
    <tr>
      <td class="labelFont">Arkusze dwustronne:</td>
      <td class="itemFont">2735</td>
    </tr>
  </table>
</div>

What I need is just to show a number after "Wszystkie odbitki" like: Wszystkie odbitki: 49946

I know it is possible in simple_html_dom but I don't know how to do it using PHP.

Code:

require_once('simple_html_dom.php');

$html = file_get_html('http://127.0.0.1/tabela.html');

$table = null;
$needle = 'Odbitki';
foreach($html->find('h3') as $marker) {
  if ($marker->innertext == $needle) {
    $table = $marker->next_sibling();
    break;
  }
}
$data = array();
if ($table) {
  foreach($table->children() as $k => $tr) {
    foreach($tr->children as $td) {
      $data[$k][] = $td->innertext;
    }
  }
}
echo '<pre>';
print_r($data);

Solution

  • According to provided html structure you need to change this line:

    if($marker->innertext == $needle) {
        $table = $marker
            // `next_sibling` gets `div class="subtitleBottomEdge"`
            ->next_sibling()
            // `next_sibling` gets `<div class="pad10">`
            ->next_sibling()
            // `first_child` gives you a required table
            ->first_child();
        break;
    }
    

    Update for one cell, for example:

    foreach($table->children() as $k => $tr) {
        $data[$k][] = $tr
            // `first_child`  gets first `td`
            ->first_child()
            // `next_sibling`  gets second `td`
            ->next_sibling()
            ->innertext;
    }