Revision [23707]
This is an old revision of PLG-Csv made by ThePLG on 2019-09-03 06:49:51.
CSV 2 Table formatter
See Also
- NoDefaultCodeClass - CSS class workaround.
- ShowCsv - HandleCsvData - More advanced code by NilsLindenberg.
This formatter converts inline csv data into a table.
Features
- Rows of the table alternate background color
- Column alignment: left, right, center or default
- Table headers/footers using wiki style heading markers
- Add comments in between data by starting a line with (#); blank lines are ignored
- Cells can be surrounded in double quotes (")
- It is possible to escape semi-colons with a backslash (\), that should appear in text
- Camel links in cell text are linked to their wiki internal pages
Example
- The code:
%%(csv)
;***a***;"***b***";***c***
***1***;d;"e";f
#test
***2***;g;;h
***3***;I;J;"K"
%%
- Would give this as result:
a | b | c | |
1 | d | e | f |
2 | g | h | |
3 | I | J | K |
Installation
- Copy the code below into a file named formatters/csv.php
- And give it the same file permissions as the other files in that directory.
Code
<?php
// convert inline csv data into a table.
// by OnegWR, may 2005, license GPL
$csv_printheader=0;
foreach(split("\n", $text) as $csv_n => $csv_line){
if(preg_match("/^#|^\s*$/",$csv_line)) continue;
if( $csv_printheader == 0){ print "<table cellpadding=\"5\" cellspacing=\"1\"><tbody>\n"; $csv_printheader=1; }
print ($csv_n%2) ? "<tr bgcolor=\"#ffffee\">" : "<tr bgcolor=\"#eeeeee\">";
foreach(split(";", $csv_line) as $csv_nn => $csv_cell){
if(preg_match("/^\s*$/",$csv_cell)){
print "<td> </td>";
}elseif(preg_match("/^(\"?)\*\*\*(.*?)\*\*\*(\"?)$/",$csv_cell,$matches)){
print "<td bgcolor=\"#cccccc\">".
$this->htmlspecialchars_ent($matches[2])."</td>";
}elseif(preg_match("/^(\"?)(.*?)(\"?)$/",$csv_cell,$matches)){
print "<td>".$this->htmlspecialchars_ent($matches[2])."</td>";
}else{
print "<td>".$this->htmlspecialchars_ent($csv_cell)."</td>";
}
}
print "</tr>\n";
}
if( $csv_printheader == 1) print "</tbody></table>\n";
?>
// convert inline csv data into a table.
// by OnegWR, may 2005, license GPL
$csv_printheader=0;
foreach(split("\n", $text) as $csv_n => $csv_line){
if(preg_match("/^#|^\s*$/",$csv_line)) continue;
if( $csv_printheader == 0){ print "<table cellpadding=\"5\" cellspacing=\"1\"><tbody>\n"; $csv_printheader=1; }
print ($csv_n%2) ? "<tr bgcolor=\"#ffffee\">" : "<tr bgcolor=\"#eeeeee\">";
foreach(split(";", $csv_line) as $csv_nn => $csv_cell){
if(preg_match("/^\s*$/",$csv_cell)){
print "<td> </td>";
}elseif(preg_match("/^(\"?)\*\*\*(.*?)\*\*\*(\"?)$/",$csv_cell,$matches)){
print "<td bgcolor=\"#cccccc\">".
$this->htmlspecialchars_ent($matches[2])."</td>";
}elseif(preg_match("/^(\"?)(.*?)(\"?)$/",$csv_cell,$matches)){
print "<td>".$this->htmlspecialchars_ent($matches[2])."</td>";
}else{
print "<td>".$this->htmlspecialchars_ent($csv_cell)."</td>";
}
}
print "</tr>\n";
}
if( $csv_printheader == 1) print "</tbody></table>\n";
?>
History
- 2005-05-09 Published on main site - OnegWR
- 2019-09-03 Code clean-up, remove deprecated code, more features, fix bugs - ThePLG
TODO
- text in quotes needs to be preserved e.g., whitespace, no splitting on semi-colons
- camel links are really hard to see
- Add more cell formatting options: alignment, bold, italic, ...
- Find a way to implement a "\n" in a cell (every call to $this->Format() is another include of formatters/wakka.php, so I would like to find another way...)
- Use more CSS classes
CategoryUserContributions