簡體   English   中英

如何在 php 的控制台上制作 alignment

[英]How to make alignment on console in php

我正在嘗試通過 PHP 中的命令提示符運行腳本,並嘗試以表格形式顯示結果。 但是由於單詞的字符長度不同,我無法正確對齊顯示結果。

我想要這樣的結果

Book                  ISBN      Department
Operating System      101       CS
C                     102       CS
java                  103       CS

誰能幫我在控制台上的 php 中獲得這個 output 。

提前致謝

如果您不想(或由於某種原因不允許)使用庫,您可以使用標准的 php printf / sprintf函數。

他們的問題是,如果您的值具有可變且不受限制的寬度,那么您將不得不決定是否截斷長值或破壞表的布局。

第一種情況:

// fixed width
$mask = "|%5.5s |%-30.30s | x |\n";
printf($mask, 'Num', 'Title');
printf($mask, '1', 'A value that fits the cell');
printf($mask, '2', 'A too long value the end of which will be cut off');

output 是

|  Num |Title                          | x |
|    1 |A value that fits the cell     | x |
|    2 |A too long value the end of wh | x |

第二種情況:

// only min-width of cells is set
$mask = "|%5s |%-30s | x |\n";
printf($mask, 'Num', 'Title');
printf($mask, '1', 'A value that fits the cell');
printf($mask, '2', 'A too long value that will brake the table');

在這里我們得到

|  Num |Title                          | x |
|    1 |A value that fits the cell     | x |
|    2 |A too long value that will brake the table | x |

如果這兩者都不能滿足您的需求並且您確實需要一個具有流動寬度列的表格,那么您必須計算每列中值的最大寬度。 但這就是PEAR::Console_Table的工作原理。

您可以使用PEAR::Console_Table

Console_Table幫助您在終端/外殼/控制台上顯示表格數據。

例子:

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 |
+----------+------+

您最好的選擇是使用 Pear Package Console_Table ( http://pear.php.net/package/Console_Table/ )。

要使用 - 在控制台上,您需要安裝 pear package,運行:

pear install Console_Table

這應該下載 package 並安裝。 然后,您可以使用示例腳本,例如:

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();

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 .

例子:

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 |
+----------+------+

其 github 頁面上查看更多示例用法。

CLIFramework 表格生成器可幫助您輕松完成工作,它支持文本 alignment、文本顏色、背景顏色、文本換行、文本溢出處理等

這是教程: https://github.com/c9s/CLIFramework/wiki/Using-Table-Component

示例代碼: https://github.com/c9s/CLIFramework/blob/master/example/table.php

use CLIFramework\Component\Table\Table;

$table = new Table;
$table->setHeaders([ 'Published Date', 'Title', 'Description' ]);
$table->addRow(array( 
    "September 16, 2014",
    "Title",
    "Description",
    29.5
));
$table->addRow(array( 
    "November 4, 2014",
    "Hooked: How to Build Habit-Forming Products",
    ["Why do some products capture widespread attention whil..."],
    99,
));
echo $table->render();

太舊了,但我現在也經歷了同樣的事情並使用了 str_pad,只需將長度設置為列的大小即可

問候。

以防萬一有人想在 PHP 中這樣做,我在 Github 上發布了一個要點

https://gist.github.com/redestructa/2a7691e7f3ae69ec5161220c99e2d1b3

只需調用:

$output = $tablePrinter->printLinesIntoArray($items, ['title', 'chilProp2']);

如果您使用的是早於 7.2 的 php 版本,則可能需要調整代碼

之后根據您的環境調用 echo 或 writeLine 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM