Skip to content

Instantly share code, notes, and snippets.

@dapepe
Last active April 19, 2024 21:53
Show Gist options
  • Save dapepe/9956717 to your computer and use it in GitHub Desktop.
Save dapepe/9956717 to your computer and use it in GitHub Desktop.
Class to generate a Markdown-style table from a PHP array
<?php
/**
* Creates a markdown document based on the parsed documentation
*
* @author Peter-Christoph Haider <peter.haider@zeyon.net>
* @package Apidoc
* @version 1.00 (2014-04-04)
* @license GNU Lesser Public License
*/
class TextTable {
/** @var int The source path */
public $maxlen = 50;
/** @var array The source path */
private $data = array();
/** @var array The source path */
private $header = array();
/** @var array The source path */
private $len = array();
/** @var array The source path */
private $align = array(
'name' => 'L',
'type' => 'C'
);
/**
* @param array $header The header array [key => label, ...]
* @param array $content Content
* @param array $align Alignment optios [key => L|R|C, ...]
*/
public function __construct($header=null, $content=array(), $align=false) {
if ($header) {
$this->header = $header;
} elseif ($content) {
foreach ($content[0] as $key => $value)
$this->header[$key] = $key;
}
foreach ($this->header as $key => $label) {
$this->len[$key] = strlen($label);
}
if (is_array($align))
$this->setAlgin($align);
$this->addData($content);
}
/**
* Overwrite the alignment array
*
* @param array $align Alignment optios [key => L|R|C, ...]
*/
public function setAlgin($align) {
$this->align = $align;
}
/**
* Add data to the table
*
* @param array $content Content
*/
public function addData($content) {
foreach ($content as &$row) {
foreach ($this->header as $key => $value) {
if (!isset($row[$key])) {
$row[$key] = '-';
} elseif (strlen($row[$key]) > $this->maxlen) {
$this->len[$key] = $this->maxlen;
$row[$key] = substr($row[$key], 0, $this->maxlen-3).'...';
} elseif (strlen($row[$key]) > $this->len[$key]) {
$this->len[$key] = strlen($row[$key]);
}
}
}
$this->data = $this->data + $content;
return $this;
}
/**
* Add a delimiter
*
* @return string
*/
private function renderDelimiter() {
$res = '|';
foreach ($this->len as $key => $l)
$res .= (isset($this->align[$key]) && ($this->align[$key] == 'C' || $this->align[$key] == 'L') ? ':' : ' ')
.str_repeat('-', $l)
.(isset($this->align[$key]) && ($this->align[$key] == 'C' || $this->align[$key] == 'R') ? ':' : ' ')
.'|';
return $res."\r\n";
}
/**
* Render a single row
*
* @param array $row
* @return string
*/
private function renderRow($row) {
$res = '|';
foreach ($this->len as $key => $l) {
$res .= ' '.$row[$key].($l > strlen($row[$key]) ? str_repeat(' ', $l - strlen($row[$key])) : '').' |';
}
return $res."\r\n";
}
/**
* Render the table
*
* @param array $content Additional table content
* @return string
*/
public function render($content=array()) {
$this->addData($content);
$res = $this->renderRow($this->header)
.$this->renderDelimiter();
foreach ($this->data as $row)
$res .= $this->renderRow($row);
return $res;
}
}
@Jevin23
Copy link

Jevin23 commented Sep 4, 2015

Hello!

how to use this class?

this code

$test_array[0] = array(
    'col1' => 'value1',
    'col2' => 'value2',
    'col3' => 'value3',
    'col4' => 'value4'
);
$test_array[1] = array(
    'col1' => 'value11',
    'col2' => 'value12',
    'col3' => 'value13',
    'col4' => 'value14'
);

$tt = new TextTable();
# $zz= tt->addData($test_array);
$aa = $tt->render($test_array);
echo $aa;

and this code

$test_array[0] = array('value1','value2','value3','value4');
$test_array[1] = array('value11','value12','value13','value14');

$tt = new TextTable();
# $zz= tt->addData($test_array);
$aa = $tt->render($test_array);
echo $aa;

output:

|
|
|
|

@fyrye
Copy link

fyrye commented Jul 14, 2016

Looks like you use the object in many ways

$columns = ['heading1','heading2','heading3'];
$rows = [
    ['a', 'b', 'c'],
    ['d', 'e', 'f']
];
$t = new TextTable($columns, $rows);
$t->addData(['extra1' => ['j','k','l']]);
echo $t->render(['extra2' => ['g','h','i']]);
$t->setAlgin(['L', 'C', 'R']);
echo \PHP_EOL;
echo $t->render();

Result:

| heading1 | heading2 | heading3 |
| -------- | -------- | -------- |
| a        | b        | c        |
| d        | e        | f        |
| j        | k        | l        |
| g        | h        | i        |
| heading1 | heading2 | heading3 |
|:-------- |:--------:| --------:|
| a        | b        | c        |
| d        | e        | f        |
| j        | k        | l        |
| g        | h        | i        |

@jonasof
Copy link

jonasof commented Mar 5, 2017

Can you publish this class in packagist?

@alexbowers
Copy link

@dapepe There is a typo in setAlign.

@eness
Copy link

eness commented Feb 12, 2021

For those having issus with utf-8 chars, change strlen functions to mb_strlen equivalent.

@athphane
Copy link

This has saved me today. Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment