简体   繁体   中英

Error/bug PHP SIMPLE HTML DOM PARSER

This is my table:

<table class="datatable"><thead></thead><thead>
<th scope="col"><font><font>Position</font></font></th>
<th scope="col">Group</th>
<th scope="col">Drivers</th>
<th scope="col">Time</th>
<th scope="col"><font><font>Points</font></font></th>
</tr></thead>
<tbody><tr><td><font><font>1°</font></font></td><td><font><font>GROUP</font></font></td><td><a href=“#”>AA, </a><a href="#">BB, </a><a href="#">CC, </a><a href="http://#">DD</a></td><td><font><font>00’11’’22</font></font></td><td><center><font><font>1111</font></font></center></td></tr><tr><td class="alt"><font><font>2°</font></font></td><td class=“alt”><font><font>GROUP</font></font></td><td><a href=“#”>AA, </a><a href="#">BB, </a><a href="#">CC, </a><a href="http://#">DD</a></td><td class="alt"><font><font>00’11’’22</font></font></td><td class="alt"><center><font><font>1111</font></font></center></td></tr><tr><td><font><font>3°</font></font></td><td><font><font>GROUP</font></font></td><td><a href=“#”>AA, </a><a href="#">BB, </a><a href="#">CC, </a><a href="http://#">DD</a></td><td><font><font>00’11’’22</font></font></td><td><center><font><font>1111</font></font></center></td></tr></tbody></table>

I can't get just the coloumn of position, time and points, I used PHP SIMPLE DOM PARSER, but the problem is when i do a foreach for tr, he repeat so many times the results and don't get the single row, try the code please:

$html = file_get_html($url);
$e = $html->find('table');
$str = str_get_html($e);
foreach($str->find('tr') as $key=>$g){
            if($key == 0)
                continue;
            echo $g;
    }
$html->clear();
    unset($html);

my code is this try it.

try this, it will give you an array containing each row in an associative array

<?php
include('simple_html_dom.php');
$html = file_get_html('table-3.html');

$data = array();
foreach($html->find('table tr') as $row) {
    $dataRow = array();
    $dataRow['position'] = $row->find('td',0)->plaintext;
    $dataRow['group'] = $row->find('td',1)->plaintext;
    $dataRow['drivers'] = $row->find('td',2)->plaintext;
    $dataRow['time'] = $row->find('td',3)->plaintext;
    $dataRow['points'] = $row->find('td',4)->plaintext;
    $data[] = $dataRow;
}

print_r($data);

$html->clear();
unset($html);

If in my case have a bad html to parse, just solved this problem thanks to

strip_tags(

function, for clean html tag wrong or useless, and thanks to that I clean the code, for finally parse without problems.

Before clean I just saved DOM with

$str->save('result.htm');

Thanks to that, I found the problem and fixed, with strip_tags!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM