Search code examples
phpdomdocument

PHP loadHTMLFile(table.html) errors


This is my script to load data from an old table which is to say it one way "un-editable".

$document = new DOMDocument();
$document -> loadHTMLFile('http://www.table.html');
$rows   = $document -> getElementsByTagName('tr');

After that I load the <td>'s into an array:

    $numRow = 0;
    foreach($rows as $result){

// 15 columns to load.
        for($i = 0; $i < 15; $i++){
        $array[$numRow][$i] = $result
        ->getElementsByTagName('td')
        ->item($i)
        ->nodeValue;
        }
    $numRow++;
    }

So far all went fine, but then the problems started. All the rows have a string column for "description" and in the description there's frequently human mistype errors like for example:

this is a cell: <td>Suppor<</td> (somebody mistiped here IDK how) error: Tag o invalid

Or when cells contain "&" like this one:

Another example: <td>From A&A Limited.</td> error: htmlParseEntityRef: expecting ';'

Errors always reffer to this line $document -> loadHTMLFile('http://www.table.html');

As you can see the table has 15 columns being $i = 4 the string column. I'm not a really PRO programmer and didn't really see this coming. Thanks in advance to any person that can give me a hand.


Solution

  • I found the solution doing this:

    @$document -> loadHTMLFile('http://www.table.html');
    $rows   = $document -> getElementsByTagName('tr');
    
    $nodeListLength = htmlentities($rows->length);
    for ($i = 0; $i < $nodeListLength; $i ++)
    {
        $node = $gastos->item(0);
        //-> VALORES A MOSTRAR EN TU QUERY
    }
    

    I hope it can help another person with the same problem. Thanks to the people that check my problem.