Last active
December 16, 2015 06:39
-
-
Save JodiTheTigger/5393475 to your computer and use it in GitHub Desktop.
CsvToHtml5Table converts a csv file to a html5 based table. It has options to ignore columns and treat values in columns as picture sources or links (http and email).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<?php | |
/* | |
CsvToHtml5Table - Coverts a CSV file to a HTML5 table | |
Copyright (C) 2013 Richard Maxwell | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU Affero General Public License as | |
published by the Free Software Foundation, either version 3 of the | |
License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU Affero General Public License for more details. | |
You should have received a copy of the GNU Affero General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
function CsvToHtml5Table($fileName, $columnsToSkip, $pictureColumns, $linkColumns) | |
{ | |
$htmlCode = '<table id="' . pathinfo($fileName, PATHINFO_FILENAME) . '">'; | |
ini_set('auto_detect_line_endings',TRUE); | |
$handle = fopen($fileName,'r'); | |
if ( $handle !== FALSE ) | |
{ | |
$doneHead = 0; | |
$headerCount = 0; | |
while ( ($data = fgetcsv($handle) ) !== FALSE ) | |
{ | |
if ($doneHead == 0) | |
{ | |
$htmlCode .= "<thead><tr>\n"; | |
$column = 1; | |
foreach ($data as $header) | |
{ | |
if (!in_array($column, $columnsToSkip)) | |
{ | |
$htmlCode .= " <td>" . htmlspecialchars(trim($header)) . "</td>\n"; | |
} | |
$column = $column + 1; | |
} | |
$htmlCode .= "</tr></thead>\n"; | |
$htmlCode .= "<tbody>\n"; | |
$doneHead = 1; | |
$headerCount = count($data); | |
} | |
else | |
{ | |
if ($headerCount == count($data)) | |
{ | |
$htmlCode .= " <tr>\n"; | |
$column = 1; | |
foreach ($data as $datum) | |
{ | |
$trimmed = trim($datum); | |
if (!in_array($column, $columnsToSkip)) | |
{ | |
if (in_array($column, $pictureColumns)) | |
{ | |
$pictureName = str_replace(' ', '', $trimmed); | |
$htmlCode .= " <td><img src=" . '"' . htmlspecialchars($pictureName) . '"' . " /></td>\n"; | |
} | |
else | |
{ | |
if (in_array($column, $linkColumns)) | |
{ | |
$pretag = "http://"; | |
// email address? | |
if (preg_match('|^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $trimmed)) | |
{ | |
$pretag = "mailto:"; | |
} | |
$htmlCode .= ' <td><a href="' . $pretag . $trimmed . '">' . $trimmed . "</a></td>\n"; | |
} | |
else | |
{ | |
$htmlCode .= " <td>" . htmlspecialchars($trimmed) . "</td>\n"; | |
} | |
} | |
} | |
$column++; | |
} | |
$htmlCode .= " </tr>\n"; | |
} | |
} | |
} | |
fclose($handle); | |
} | |
ini_set('auto_detect_line_endings',FALSE); | |
$htmlCode .= "</tbody>\n"; | |
$htmlCode .= "</table>\n"; | |
return $htmlCode; | |
} | |
?> | |
<html> | |
<head> | |
<title>CSV to HTML5 Table Generator</title> | |
</head> | |
<body> | |
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data"> | |
<table> | |
<tbody> | |
<tr><td colspan="10">Ignore Columns:</td></tr> | |
<tr> | |
<td><input type="checkbox" name="skip[]" value="1">1</td> | |
<td><input type="checkbox" name="skip[]" value="2">2</td> | |
<td><input type="checkbox" name="skip[]" value="3">3</td> | |
<td><input type="checkbox" name="skip[]" value="4">4</td> | |
<td><input type="checkbox" name="skip[]" value="5">5</td> | |
<td><input type="checkbox" name="skip[]" value="6">6</td> | |
<td><input type="checkbox" name="skip[]" value="7">7</td> | |
<td><input type="checkbox" name="skip[]" value="8">8</td> | |
<td><input type="checkbox" name="skip[]" value="9">9</td> | |
<td><input type="checkbox" name="skip[]" value="10">10</td> | |
</tr> | |
<tr><td colspan="10">Picture refs:</td></tr> | |
<tr> | |
<td><input type="checkbox" name="picture[]" value="1">1</td> | |
<td><input type="checkbox" name="picture[]" value="2">2</td> | |
<td><input type="checkbox" name="picture[]" value="3">3</td> | |
<td><input type="checkbox" name="picture[]" value="4">4</td> | |
<td><input type="checkbox" name="picture[]" value="5">5</td> | |
<td><input type="checkbox" name="picture[]" value="6">6</td> | |
<td><input type="checkbox" name="picture[]" value="7">7</td> | |
<td><input type="checkbox" name="picture[]" value="8">8</td> | |
<td><input type="checkbox" name="picture[]" value="9">9</td> | |
<td><input type="checkbox" name="picture[]" value="10">10</td> | |
</tr> | |
<tr><td colspan="10">HTML Links:</td></tr> | |
<tr> | |
<td><input type="checkbox" name="htmllinks[]" value="1">1</td> | |
<td><input type="checkbox" name="htmllinks[]" value="2">2</td> | |
<td><input type="checkbox" name="htmllinks[]" value="3">3</td> | |
<td><input type="checkbox" name="htmllinks[]" value="4">4</td> | |
<td><input type="checkbox" name="htmllinks[]" value="5">5</td> | |
<td><input type="checkbox" name="htmllinks[]" value="6">6</td> | |
<td><input type="checkbox" name="htmllinks[]" value="7">7</td> | |
<td><input type="checkbox" name="htmllinks[]" value="8">8</td> | |
<td><input type="checkbox" name="htmllinks[]" value="9">9</td> | |
<td><input type="checkbox" name="htmllinks[]" value="10">10</td> | |
</tr> | |
<tr><td colspan="10">Select file</td></tr> | |
<tr><td colspan="10"><input type="file" name="file" id="file"/></td></tr> | |
<tr><td colspan="10">Submit</td></tr> | |
<tr><td colspan="10"><input type="submit" name="submit" /></td></tr> | |
</tbody> | |
</table> | |
</form> | |
<?php | |
if ( isset($_POST["submit"]) ) | |
{ | |
if ( isset($_FILES["file"])) | |
{ | |
//if there was an error uploading the file | |
if ($_FILES["file"]["error"] > 0) | |
{ | |
echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; | |
} | |
else | |
{ | |
$htmlCode = ""; | |
//Print file details | |
echo "Upload: " . $_FILES["file"]["name"] . "<br />"; | |
echo "Type: " . $_FILES["file"]["type"] . "<br />"; | |
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; | |
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; | |
echo "Extenstion: " . pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION) . "<br />"; | |
echo "Filename: " . pathinfo($_FILES['file']['name'], PATHINFO_FILENAME) . "<br />"; | |
// Parse the skip columns | |
$skip = $_POST['skip']; | |
// Parse the picture columns | |
$picture = $_POST['picture']; | |
// Aaaaand parse the html link columns | |
$htmllinks = $_POST['htmllinks']; | |
// Parse file, print it out. | |
$htmlCode .= '<table id="' . pathinfo($_FILES['file']['name'], PATHINFO_FILENAME) . '">'; | |
// test for size and filetype, less than 100k for now. | |
if ($_FILES["file"]["size"] < (1024*100)) | |
{ | |
if (strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)) == "csv") | |
{ | |
$code = CsvToHtml5Table($_FILES["file"]["tmp_name"], $skip, $picture, $htmllinks); | |
// Debug: spit it out. | |
echo "<pre>\n"; | |
echo htmlspecialchars($code); | |
echo "\n</pre>"; | |
} | |
else | |
{ | |
echo "<p>File has to be a .csv file</p>"; | |
} | |
} | |
else | |
{ | |
echo "<p>CSV file is too big sorry.</p>"; | |
} | |
} | |
} | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment