I get this error:
Fatal error: Call to a member function getElementsByTagName() on a non-object in /XXX on line 17
This is my PHP:
$html = file_get_contents('../test.html');
$doc = new DOMDocument();
$doc->loadHTML($html);
$titles = array();
$table = $doc->getElementById('my_table');
$a = 0;
$delim = "";
$csv = "";
//number of columns in the table
$num_cols = 4;
foreach($table->getElementsByTagName('td') as $td) {
if($a == 0) {
$delim = "";
}
if($a < $num_cols) {
$csv .= $delim . '"' . $td->nodeValue. '"';
$delim = ";";
}
if($a == ($num_cols-1)) {
$csv .= "\n";
$a = -1;
}
$a++;
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=wm_export.csv");
echo $csv;
exit;
?>
And this my HTML file ../text.html
<table id="my_table" style="width: 600px;">
<tbody>
<tr>
<td>Name</td>
<td>Surname</td>
<td>Address</td>
<td>City</td>
</tr>
<tr>
<td>Alfred</td>
<td>William</td>
<td>327 Cranbrook Road</td>
<td>London</td>
</tr>
<tr>
<td>Mario</td>
<td>Rossi</td>
<td>via Genova</td>
<td>Milano</td>
</tr>
<tr>
<td>Eddy</td>
<td>Smitt</td>
<td>Cranbrook Road</td>
<td>Toronto</td>
</tr>
</tbody>
</table>