Pergunta

Im new to PHP, so im a little stuck in this code ex. if U look at this Link there is a table, im trying to get only that table out and I know its the first table and its comming on source code line 1065.

But i get this error

bool(false)
Fatal error: Call to a member function getElementsByTagName() on a non-object in /get.php on line 23

I have this code, hope someone can guide me. (line 23 is the $rows..line)

<?php
$pulje = '163532';
$url = "http://www.dbu.dk/turneringer_og_resultater/resultatsoegning/position.aspx?poolid=$pulje";

// enable user error handling
var_dump(libxml_use_internal_errors(true));

// parse the html into DOMDoc.
$dom = new domDocument();

$dom->recover = true;
$dom->strictErrorChecking = false;

$dom->loadHTML($url);

$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(count($tables-1))->getElementsByTagName('tr');

$array = array();
foreach($rows as $row){
    $cols = $row->getElementsByTagName('td');
    echo $array[] = $cols;
}
?>

....UPDATE.... I updated the code frem loadHTML to loadHTMLFile and then i now get this error

bool(false) Catchable fatal error: Object of class DOMNodeList could not be converted to string in /get.php on line 28

Line 28 is the echo $array....

Foi útil?

Solução

You should use DOMDocument::loadHTMLFile instead of DOMDocument::loadHTML

  • DOMDocument::loadHTML : Load HTML from a string
  • DOMDocument::loadHTMLFile : Load HTML from a file

http://php.net/manual/en/domdocument.loadhtml.php http://php.net/manual/en/domdocument.loadhtmlfile.php

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top