I have been pulling my hair above below snippet of code – which is syntatically correct.. However I keep getting the following error when I put it on the server:
Parse error: syntax error, unexpected T_VARIABLE in /home/scripts/temp.php5 on line 3
Line 3:
Line 3 = $dom = new DOMDocument();
Any ideas what i could be doing wrong?
<?php
// new dom object
$dom = new DOMDocument();
//load the html
$html = $dom->loadHTMLFile('<HTML><A HREF="ss">asd</A>');
//discard white space
$dom->preserveWhiteSpace = false;
//the table by its tag name
$tables = $dom->getElementsByTagName('table');
//get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');
// loop over the table rows
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
echo $cols->item(0)->nodeValue.'';
echo $cols->item(1)->nodeValue.'';
echo $cols->item(2)->nodeValue;
}
?>
I tried that code and got the following error
PHP Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/prog/www/imdb/test.php on line 15
i modified the code to
i’m guessing that there’s an error before the lines you have pasted. what version of php and what operating system.
is the file you have pasted temp.php5 and what file calls the file is it included or just procedural php