Andreas Grech
Andreas Grech

Reputation: 108000

Colorizing Windows command line output from PHP

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?

Upvotes: 26

Views: 22156

Answers (7)

Rubén Sousa
Rubén Sousa

Reputation: 11

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.

Upvotes: 0

seven
seven

Reputation: 2607

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";

Upvotes: 6

maix
maix

Reputation: 81

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

Upvotes: 7

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

Upvotes: 1

user904550
user904550

Reputation:

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

Upvotes: 10

nickf
nickf

Reputation: 546303

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.

Upvotes: 2

red
red

Reputation: 3233

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.

Upvotes: 0

Related Questions