对 PHP 的 Windows 命令行输出进行着色

2021-12-25 00:00:00 colors cmd php

要在 bash 中输出彩色文本,您可以使用 ANSI 转义序列.

To output colored text in bash, you use ANSI escape sequences.

如何在 Windows 命令行上输出彩色文本,特别是从 PHP 输出?

How do you output colored text on a Windows command line, specifically from PHP?

推荐答案

从以下位置下载 dynwrap.dll:http://www.script-coding.com/dynwrap95.zip

Download dynwrap.dll from : http://www.script-coding.com/dynwrap95.zip

然后将其解压到%systemroot%system32目录,然后在命令行中运行以下命令:

Then extract it to %systemroot%system32 directory and then run following command in command line:

regsvr32.exe "%systemroot%system32dynwrap.dll"

您将收到一条成功消息,表示 dynwrap.dll 已注册.

You'll get a success message which means dynwrap.dll is registered.

那么你可以这样使用它:

Then you can use it this way :

$com = new COM('DynamicWrapper');

// register needed features
$com->Register('kernel32.dll', 'GetStdHandle', 'i=h', 'f=s', 'r=l');
$com->Register('kernel32.dll', 'SetConsoleTextAttribute', 'i=hl', 'f=s', 'r=t');

// get console handle
$ch = $com->GetStdHandle(-11);

一些例子:

$com->SetConsoleTextAttribute($ch, 4);
echo 'This is a red text!';
$com->SetConsoleTextAttribute($ch, 7);
echo 'Back to normal color!';

颜色代码:
7 => 默认
0 => 黑色
1 => 蓝色
2 => 绿色
3 => 水色
4 => 红色
5 => 紫色
6 => 黄色
7 => 浅灰色
8 => 灰色
9 => 浅蓝色
10 => 浅绿色
11 => 浅水色
12 => 浅红色
13 => 浅紫色
14 => 浅黄色
15 => 白色

colors codes:
7 => default
0 => black
1 => blue
2 => green
3 => aqua
4 => red
5 => purple
6 => yellow
7 => light gray
8 => gray
9 => light blue
10 => light green
11 => light aqua
12 => light red
13 => light purple
14 => light yellow
15 => white

相关文章