The converter allows you to convert an array into an ASCII formatted string. You can see such ASCII formatted tables when working in command line with MySQL or PostgreSQL.
ToolkitLab\ASCII\Formatter\MysqlFormatter
+---+-------+----------+
| | A | B |
+---+-------+----------+
| 1 | Name | Position |
| 2 | John | Writer |
| 3 | Anna | Student |
| 4 | David | Teacher |
+---+-------+----------+
ToolkitLab\ASCII\Formatter\UnicodeFormatter
╔═══╦═══════╦══════════╗
║ ║ A ║ B ║
╠═══╬═══════╬══════════╣
║ 1 ║ Name ║ Position ║
║ 2 ║ John ║ Writer ║
║ 3 ║ Anna ║ Student ║
║ 4 ║ David ║ Teacher ║
╚═══╩═══════╩══════════╝
ToolkitLab\ASCII\Formatter\DotsFormatter
........................
: : A : B :
:...:.......:..........:
: 1 : Name : Position :
: 2 : John : Writer :
: 3 : Anna : Student :
: 4 : David : Teacher :
:...:.......:..........:
ToolkitLab\ASCII\Formatter\MarkdownFormatter
| | A | B |
|---|-------|----------|
| 1 | Name | Position |
| 2 | John | Writer |
| 3 | Anna | Student |
| 4 | David | Teacher |
ToolkitLab\ASCII\Formatter\TableFormatter
___ _______ __________
| | A | B |
|___|_______|__________|
| 1 | Name | Position |
| 2 | John | Writer |
| 3 | Anna | Student |
| 4 | David | Teacher |
¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
$formatter = new MysqlFormatter();
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
this will output the following:
+-------+----------+
| Name | Position |
| John | Writer |
| Anna | Student |
| David | Teacher |
+-------+----------+
There are different modes available, which add additional formatting to the output:
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::HEADER_FIRST_ROW_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+-------+----------+
| Name | Position |
+-------+----------+
| John | Writer |
| Anna | Student |
| David | Teacher |
+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::HEADER_NUMERIC_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+-------+----------+
| 1 | 2 |
+-------+----------+
| Name | Position |
| John | Writer |
| Anna | Student |
| David | Teacher |
+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::HEADER_ABC_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+-------+----------+
| A | B |
+-------+----------+
| Name | Position |
| John | Writer |
| Anna | Student |
| David | Teacher |
+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::SIDEBAR_NUMERIC_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+---+-------+----------+
| 1 | Name | Position |
| 2 | John | Writer |
| 3 | Anna | Student |
| 4 | David | Teacher |
+---+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::SIDEBAR_ABC_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+---+-------+----------+
| A | Name | Position |
| B | John | Writer |
| C | Anna | Student |
| D | David | Teacher |
+---+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::SPREADSHEET_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+---+-------+----------+
| | A | B |
+---+-------+----------+
| 1 | Name | Position |
| 2 | John | Writer |
| 3 | Anna | Student |
| 4 | David | Teacher |
+---+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
use ToolkitLab\ASCII\AbstractFormatter;
$formatter = new MysqlFormatter([
'mode' => AbstractFormatter::SIDEBAR_NUMERIC_MODE | AbstractFormatter::HEADER_NUMERIC_MODE
]);
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
]);
?>
will output:
+---+-------+----------+
| | 1 | 2 |
+---+-------+----------+
| 1 | Name | Position |
| 2 | John | Writer |
| 3 | Anna | Student |
| 4 | David | Teacher |
+---+-------+----------+
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
$formatter = new MysqlFormatter();
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
], [
'rotate' => -90
]);
?>
will output:
+----------+--------+---------+---------+
| Position | Writer | Student | Teacher |
| Name | John | Anna | David |
+----------+--------+---------+---------+
The parameter "max_cell_length" allows you to set the maximum cell length. If the length is exceeded all further text will be replaced by three dots (the ending can be set with the parameter "max_cell_ending"). Default value is 100.
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
$formatter = new MysqlFormatter();
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
], [
'max_cell_length' => 5,
]);
?>
will output:
+-------+----------+
| Name | Posit... |
| John | Write... |
| Anna | Stude... |
| David | Teach... |
+-------+----------+
The parameter "max_cell_ending" allows you to set the ending if the maximum cell length is exceeded. Default value is "...".
<?php
use ToolkitLab\ASCII\Formatter\MysqlFormatter;
$formatter = new MysqlFormatter();
echo $formatter->format([
["Name", "Position"],
["John", "Writer"],
["Anna", "Student"],
["David", "Teacher"],
], [
'max_cell_length' => 5,
'max_cell_ending' => '[hidden]',
]);
?>
will output:
+-------+---------------+
| Name | Posit[hidden] |
| John | Write[hidden] |
| Anna | Stude[hidden] |
| David | Teach[hidden] |
+-------+---------------+