项目作者: toolkitlab

项目描述 :
ASCII converter
高级语言: PHP
项目地址: git://github.com/toolkitlab/ascii-converter.git
创建时间: 2017-10-25T18:59:31Z
项目社区:https://github.com/toolkitlab/ascii-converter

开源协议:MIT License

下载


ASCII converter

Latest version
Software License
Build Status
Coverage Status
Quality Score
Total Downloads
Gitter

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.

Available converters

ToolkitLab\ASCII\Formatter\MysqlFormatter

  1. +---+-------+----------+
  2. | | A | B |
  3. +---+-------+----------+
  4. | 1 | Name | Position |
  5. | 2 | John | Writer |
  6. | 3 | Anna | Student |
  7. | 4 | David | Teacher |
  8. +---+-------+----------+

ToolkitLab\ASCII\Formatter\UnicodeFormatter

  1. ╔═══╦═══════╦══════════╗
  2. A B
  3. ╠═══╬═══════╬══════════╣
  4. 1 Name Position
  5. 2 John Writer
  6. 3 Anna Student
  7. 4 David Teacher
  8. ╚═══╩═══════╩══════════╝

ToolkitLab\ASCII\Formatter\DotsFormatter

  1. ........................
  2. : : A : B :
  3. :...:.......:..........:
  4. : 1 : Name : Position :
  5. : 2 : John : Writer :
  6. : 3 : Anna : Student :
  7. : 4 : David : Teacher :
  8. :...:.......:..........:

ToolkitLab\ASCII\Formatter\MarkdownFormatter

  1. | | A | B |
  2. |---|-------|----------|
  3. | 1 | Name | Position |
  4. | 2 | John | Writer |
  5. | 3 | Anna | Student |
  6. | 4 | David | Teacher |

ToolkitLab\ASCII\Formatter\TableFormatter

  1. ___ _______ __________
  2. | | A | B |
  3. |___|_______|__________|
  4. | 1 | Name | Position |
  5. | 2 | John | Writer |
  6. | 3 | Anna | Student |
  7. | 4 | David | Teacher |
  8. ¯¯¯ ¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯

Usage

  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. $formatter = new MysqlFormatter();
  4. echo $formatter->format([
  5. ["Name", "Position"],
  6. ["John", "Writer"],
  7. ["Anna", "Student"],
  8. ["David", "Teacher"],
  9. ]);
  10. ?>

this will output the following:

  1. +-------+----------+
  2. | Name | Position |
  3. | John | Writer |
  4. | Anna | Student |
  5. | David | Teacher |
  6. +-------+----------+

Modes

There are different modes available, which add additional formatting to the output:

Use the first row as a header
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::HEADER_FIRST_ROW_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +-------+----------+
  2. | Name | Position |
  3. +-------+----------+
  4. | John | Writer |
  5. | Anna | Student |
  6. | David | Teacher |
  7. +-------+----------+
Use a numbered header
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::HEADER_NUMERIC_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +-------+----------+
  2. | 1 | 2 |
  3. +-------+----------+
  4. | Name | Position |
  5. | John | Writer |
  6. | Anna | Student |
  7. | David | Teacher |
  8. +-------+----------+
Use an alphabetical header
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::HEADER_ABC_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +-------+----------+
  2. | A | B |
  3. +-------+----------+
  4. | Name | Position |
  5. | John | Writer |
  6. | Anna | Student |
  7. | David | Teacher |
  8. +-------+----------+
Use a numbered sidebar
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::SIDEBAR_NUMERIC_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +---+-------+----------+
  2. | 1 | Name | Position |
  3. | 2 | John | Writer |
  4. | 3 | Anna | Student |
  5. | 4 | David | Teacher |
  6. +---+-------+----------+
Use an alphabetical sidebar
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::SIDEBAR_ABC_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +---+-------+----------+
  2. | A | Name | Position |
  3. | B | John | Writer |
  4. | C | Anna | Student |
  5. | D | David | Teacher |
  6. +---+-------+----------+
Apply spreadsheet mode
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::SPREADSHEET_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +---+-------+----------+
  2. | | A | B |
  3. +---+-------+----------+
  4. | 1 | Name | Position |
  5. | 2 | John | Writer |
  6. | 3 | Anna | Student |
  7. | 4 | David | Teacher |
  8. +---+-------+----------+
Using multiple modes
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. use ToolkitLab\ASCII\AbstractFormatter;
  4. $formatter = new MysqlFormatter([
  5. 'mode' => AbstractFormatter::SIDEBAR_NUMERIC_MODE | AbstractFormatter::HEADER_NUMERIC_MODE
  6. ]);
  7. echo $formatter->format([
  8. ["Name", "Position"],
  9. ["John", "Writer"],
  10. ["Anna", "Student"],
  11. ["David", "Teacher"],
  12. ]);
  13. ?>

will output:

  1. +---+-------+----------+
  2. | | 1 | 2 |
  3. +---+-------+----------+
  4. | 1 | Name | Position |
  5. | 2 | John | Writer |
  6. | 3 | Anna | Student |
  7. | 4 | David | Teacher |
  8. +---+-------+----------+
Rotate
  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. $formatter = new MysqlFormatter();
  4. echo $formatter->format([
  5. ["Name", "Position"],
  6. ["John", "Writer"],
  7. ["Anna", "Student"],
  8. ["David", "Teacher"],
  9. ], [
  10. 'rotate' => -90
  11. ]);
  12. ?>

will output:

  1. +----------+--------+---------+---------+
  2. | Position | Writer | Student | Teacher |
  3. | Name | John | Anna | David |
  4. +----------+--------+---------+---------+
Set max cell length

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.

  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. $formatter = new MysqlFormatter();
  4. echo $formatter->format([
  5. ["Name", "Position"],
  6. ["John", "Writer"],
  7. ["Anna", "Student"],
  8. ["David", "Teacher"],
  9. ], [
  10. 'max_cell_length' => 5,
  11. ]);
  12. ?>

will output:

  1. +-------+----------+
  2. | Name | Posit... |
  3. | John | Write... |
  4. | Anna | Stude... |
  5. | David | Teach... |
  6. +-------+----------+
Set ending for exceeded max cell length

The parameter “max_cell_ending” allows you to set the ending if the maximum cell length is exceeded. Default value is “…”.

  1. <?php
  2. use ToolkitLab\ASCII\Formatter\MysqlFormatter;
  3. $formatter = new MysqlFormatter();
  4. echo $formatter->format([
  5. ["Name", "Position"],
  6. ["John", "Writer"],
  7. ["Anna", "Student"],
  8. ["David", "Teacher"],
  9. ], [
  10. 'max_cell_length' => 5,
  11. 'max_cell_ending' => '[hidden]',
  12. ]);
  13. ?>

will output:

  1. +-------+---------------+
  2. | Name | Posit[hidden] |
  3. | John | Write[hidden] |
  4. | Anna | Stude[hidden] |
  5. | David | Teach[hidden] |
  6. +-------+---------------+