26

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

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

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 4
    Good question! Although I think `cmd.exe` is plain incapable of this. – Pekka Aug 16 '11 at 22:54
  • 1
    Ah, it seems to be possible after all. http://stackoverflow.com/questions/77744/how-can-i-change-the-text-color-in-the-windows-command-prompt – Pekka Aug 16 '11 at 22:55
  • @Pekka I saw this one too, but seems too much work for what it's worth. Though for curiosity: http://www.mailsend-online.com/blog/setting-windows-console-text-colors-in-c.html – red Aug 16 '11 at 23:29
  • 3
    I've wondered the same for years. Plain ANSI escape codes used to work fine in Windows 95/98 but they never got to NT/2000. – Álvaro González Aug 17 '11 at 09:32
  • I old old times of DOS 6.22 you'd need to load ANSI.SYS http://en.wikipedia.org/wiki/ANSI.SYS to enable ASNI escape codes in DOS prompt. – Mchl Aug 17 '11 at 15:05

7 Answers7

10

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

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

regsvr32.exe "%systemroot%\system32\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);

some example:

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

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

  • 4
    `regsvr32.exe "%systemroot%\system32\dynwrap.dll"` don't work for me on Windows 7, 64bit. This work for me instead http://softkube.com/blog/ansi-command-line-colors-under-windows/ – Sawny Nov 22 '11 at 19:49
  • @Sawny you should post this as a complete answer, it worked for me on Windows 8 x64!!! – Frederic Yesid Peña Sánchez Apr 01 '13 at 18:14
  • @Sawny - you are a life saver! I have been looking for this answer for years. Not only will your answer do colors - BUT - it will also do the rest of the ANSI stuff - like move the cursor to a new location! I just want to weep. You have no idea just how long I have been looking for this! It is a God send. Thanks a million times! – Mark Manning Jun 06 '20 at 20:51
6

ANSI escape sequences are now supported in Windows since Windows 10 1511. You can run the below code from cmd with php, and you will get the results in color if you are running Windows 10 1511 and above.

<?php
echo "\033[31mRed\n".    // Red Color Text 
     "\033[32mGreen\n".  // Green Color Text
     "\033[33mYellow\n". // Yellow Color Text
     "\033[34mBlue\n".   // Blue Color Text
     "\033[37mWhite\n";  // White Color Text
?>

I am running Windows 10 1903, and below screenshot shows my output of the script in cmd:

enter image description here

For the complete escape sequence reference:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
maix
  • 71
  • 1
  • 4
5

Windows 10 supports color in terminal natively (full reference).

Prior to Windows 10, ANSI escape codes were not available in Windows Command Prompt, natively. For older Windows you can try:

On bash shell it works like a charm and there is even a php lib for that: http://sourceforge.net/projects/milcovlib/

If it's an overkill for you you can try this:

echo "\033[31mred\033[37m\r\n";
echo "\033[32mgreen\033[37m\r\n";
echo "\033[41;30mblack on red\033[40;37m\r\n";

Here is the list of bash color codes:

$black = "33[0;30m";
$darkgray = "33[1;30m";
$blue = "33[0;34m";
$lightblue = "33[1;34m";
$green = "33[0;32m";
$lightgreen = "33[1;32m";
$cyan = "33[0;36m";
$lightcyan = "33[1;36m";
$red = "33[0;31m";
$lightred = "33[1;31m";
$purple = "33[0;35m";
$lightpurple = "33[1;35m";
$brown = "33[0;33m";
$yellow = "33[1;33m";
$lightgray = "33[0;37m";
$white = "33[1;37m";
seven
  • 2,388
  • 26
  • 28
2

It appears that using plain escape codes won't work on the Windows command prompt. The answers on the other SO questions related to this say that you need to use the Win32 API. A Win32 API library does exist for PHP, however the documentation does have a large red section warning that it is experimental. YMMV.

Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
1

You can do it with php-wcli extension. This extension use native Windows functionality instead of third party ansi emulator. https://github.com/ZmotriN/php-wcli

  • This qualifies as "link-only answer" - could you post code snippet/piece of text from linked page describing the solution? – bardzusny May 29 '15 at 07:49
0

On Windows 10 (compilation 19042.1110) PHP 7.4, this worked for me:

echo("\x1b[31m Memory Testing \x1b[0m \n\r");

The escape sequence is "\x1b["

"\x1b[31m" is for red color

"\x1b[0m" is for 'normal' color

The color codes are here

I hope to be helpful.

0

Try this:

echo chr(27); //Escape char.
echo "5;31;47"; //Graphic mode blink red on white 

Don't have a shell/php env under my hand but this should output precisely what you want.

red
  • 3,163
  • 1
  • 17
  • 16
  • 5
    This is the output I got with that: `←5;31;47` – Andreas Grech Aug 16 '11 at 23:03
  • @Andreas This seems to be the output returned on Windows systems. The provided solution only works on Linux and can't be achieved on the Windows CLI because there are only two colors (16 are available but you can only have two at the same time). – red Aug 16 '11 at 23:23
  • 3
    Yes, I specifically specified in my question that I'm looking for a Windows solution. – Andreas Grech Aug 16 '11 at 23:25
  • Sorry, the "bash" part of your question made me overlook the "windows" part. As I told Pekka, this isn't feasible by standards means. You would need to write a whole new program to output colors and then, you'd have to pipe the php output in it,... – red Aug 16 '11 at 23:31
  • As stated before: https://softkube.com/blog/ansi-command-line-colors-under-windows works. – Mark Manning Jun 06 '20 at 20:49