对 PHP 的 Windows 命令行输出进行着色 [英] Colorizing Windows command line output from PHP

查看:35
本文介绍了对 PHP 的 Windows 命令行输出进行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在 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

这篇关于对 PHP 的 Windows 命令行输出进行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆