How to make alignment on console in php

前端 未结 7 801
慢半拍i
慢半拍i 2020-12-23 17:06

I am trying to run a script through command prompt in PHP and trying to show the result in tabular form. But due to different character length of words I am not able to show

7条回答
  •  旧巷少年郎
    2020-12-23 17:33

    You could try the recent simple PHP library ConsoleTable if you don't want to use the standard PHP functions printf/sprintf or the pear package PEAR::Console_Table.

    Example:

    require_once 'ConsoleTable.php';
    
    $table = new LucidFrame\Console\ConsoleTable();
    $table
        ->addHeader('Language')
        ->addHeader('Year')
        ->addRow()
            ->addColumn('PHP')
            ->addColumn(1994)
        ->addRow()
            ->addColumn('C++')
            ->addColumn(1983)
        ->addRow()
            ->addColumn('C')
            ->addColumn(1970)
        ->display()
    ;
    

    Output:

    +----------+------+
    | Language | Year |
    +----------+------+
    | PHP      | 1994 |
    | C++      | 1983 |
    | C        | 1970 |
    +----------+------+
    

    See more example usages at its github page.

提交回复
热议问题