How to make alignment on console in php

前端 未结 7 777
慢半拍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:40

    You can use PEAR::Console_Table:

    Console_Table helps you to display tabular data on a terminal/shell/console.

    Example:

    require_once 'Console/Table.php';
    
    $tbl = new Console_Table();
    
    $tbl->setHeaders(array('Language', 'Year'));
    
    $tbl->addRow(array('PHP', 1994));
    $tbl->addRow(array('C',   1970));
    $tbl->addRow(array('C++', 1983));
    
    echo $tbl->getTable();
    

    Output:

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

提交回复
热议问题