如何在php中的控制台上进行对齐

发布于 2024-11-29 00:53:44 字数 311 浏览 3 评论 0原文

我正在尝试通过 PHP 中的命令提示符运行脚本,并尝试以表格形式显示结果。但由于单词的字符长度不同,我无法正确显示结果对齐。

我想要这样的结果

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

任何人都可以帮助我在控制台上的 php 中获得这样的输出。

提前致谢

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 the result properly align.

I want result like this

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

Can anyone please help me to get this output like this in php on console.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

郁金香雨 2024-12-06 00:53:44

如果您不想(或由于某种原因不允许)使用库,您可以使用标准 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');

输出是

|  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 的工作原理。

If you don't want (or not allowed for some reason) to use libraries, you can use standard php printf / sprintf functions.

The problem with them that if you have values with variable and non-limited width, then you will have to decide if long values will be truncated or break table's layout.

First case:

// 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');

The output is

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

Second case:

// 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');

And here we get

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

If neither of that satisfies your needs and you really need a table with flowing width columns, than you have to calculate maximum width of values in each column. But that is how PEAR::Console_Table exactly works.

网名女生简单气质 2024-12-06 00:53:44

您可以使用 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();

输出:

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

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 |
+----------+------+
月光色 2024-12-06 00:53:44

您最好的选择是使用 Pear 包 Console_Table ( http://pear.php.net/package/Console_Table /)。

要在控制台上使用 - 您需要安装 pear 软件包,运行:

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

Your best option is to use the Pear Package Console_Table ( http://pear.php.net/package/Console_Table/ ).

To use - on a console you need to install the pear package, running:

pear install Console_Table

this should download the package and install. You can then use a sample script such as:

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();
故笙诉离歌 2024-12-06 00:53:44

如果你不想使用标准的 PHP 函数,你可以尝试最近的简单 PHP 库 ConsoleTable printf/sprintf 或 pear 包 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()
;

输出:

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

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

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.

小瓶盖 2024-12-06 00:53:44

太旧了,但我现在也经历了同样的情况并使用了 str_pad,只需将长度设置为列的大小即可

Too old, but i went trough the same now and used str_pad, just set the lenght as the size of your column and thats it

regards.

幻梦 2024-12-06 00:53:44

CLIFramework 表生成器可以帮助您轻松完成工作,它支持文本对齐、文本颜色、背景颜色、文本换行、文本溢出处理等。

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

示例代码:https://github.com/c9s/CLIFramework/blob/master/example/表.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();

The CLIFramework table generator helps you get the job done very easily and it supports text alignment, text color, background color, text wrapping, text overflow handling.. etc

Here is the tutorial: https://github.com/c9s/CLIFramework/wiki/Using-Table-Component

Sample code: 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();
心不设防 2024-12-06 00:53:44

以防万一有人想用 PHP 做到这一点,我在 Github 上发布了一个要点

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

只需调用:

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

您可能需要调整代码

如果您使用的 php 版本早于 7.2,则根据您的环境调用 echo 或 writeLine 之后, 。

Just in case someone wants to do that in PHP I posted a gist on Github

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

simply call:

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

you may need to adapt the code if you are using a php version older than 7.2

after that call echo or writeLine depending on your environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文