Skip to content

Instantly share code, notes, and snippets.

@tormjens
Created April 5, 2016 10:51
Show Gist options
  • Save tormjens/a4c69742b2d735a8cfd07840397c38a0 to your computer and use it in GitHub Desktop.
Save tormjens/a4c69742b2d735a8cfd07840397c38a0 to your computer and use it in GitHub Desktop.
Convert a HTML table to array to a design element (for when client deliver some content in a word/excell table)
<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
ini_set('default_charset', 'utf-8');
$data = file_get_contents('data/data.html');
$DOM = new DOMDocument();
$DOM->loadHTML($data);
$Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');
//#Get header name of the table
foreach($Header as $NodeHeader)
{
$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
//print_r($aDataTableHeaderHTML); die();
//#Get row data/detail table without header name as key
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail)
{
$rows[$j][] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', trim($sNodeDetail->textContent));
$i = $i + 1;
$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
foreach($rows as $row) {
?>
<li>
<h2><?php echo $row[0]; ?></h2>
<p><strong>Installation/location</strong></p>
<p><?php echo nl2br($row[1]); ?></p>
<p><strong>Project</strong></p>
<p><?php echo nl2br($row[2]); ?></p>
<p><strong>Description</strong></p>
<p><?php echo nl2br($row[3]); ?></p>
<p><strong>Duration/manhours</strong></p>
<p><?php echo nl2br($row[4]); ?></p>
</li>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment