How to enable color for PHP CLI?
Solution 1
First we use an escape character so we can actually define a output color. This is done with \033
(\e). Then we open the color statement with [31m
. Red in this case.
The "some colored text" will be the text outputted in a different color. And after that we have to close the color statement with \033[0m
.
php -r 'echo "\033[31m some colored text \033[0m some white text \n";'
Solution 2
For lazier
function colorLog($str, $type = 'i'){
switch ($type) {
case 'e': //error
echo "\033[31m$str \033[0m\n";
break;
case 's': //success
echo "\033[32m$str \033[0m\n";
break;
case 'w': //warning
echo "\033[33m$str \033[0m\n";
break;
case 'i': //info
echo "\033[36m$str \033[0m\n";
break;
default:
# code...
break;
}
}
Solution 3
After doing some experiments, I made these codes:
function formatPrint(array $format=[],string $text = '') {
$codes=[
'bold'=>1,
'italic'=>3, 'underline'=>4, 'strikethrough'=>9,
'black'=>30, 'red'=>31, 'green'=>32, 'yellow'=>33,'blue'=>34, 'magenta'=>35, 'cyan'=>36, 'white'=>37,
'blackbg'=>40, 'redbg'=>41, 'greenbg'=>42, 'yellowbg'=>44,'bluebg'=>44, 'magentabg'=>45, 'cyanbg'=>46, 'lightgreybg'=>47
];
$formatMap = array_map(function ($v) use ($codes) { return $codes[$v]; }, $format);
echo "\e[".implode(';',$formatMap).'m'.$text."\e[0m";
}
function formatPrintLn(array $format=[], string $text='') {
formatPrint($format, $text); echo "\r\n";
}
//Examples:
formatPrint(['blue', 'bold', 'italic','strikethrough'], "Wohoo");
formatPrintLn(['yellow', 'italic'], " I'm invicible");
formatPrintLn(['yellow', 'bold'], "I'm invicible");
Just copy and paste the code above and... Enjoy :)
Related videos on Youtube
Barbayar Dashzeveg
Updated on October 15, 2021Comments
-
Barbayar Dashzeveg over 2 years
How do I enable the colors for output of CLI? The below one is, running on Ubuntu.
If you see the screenshot, obviously the colors is enabled for terminal. And, if I callecho
, it doesn't colorize the result, but if I useecho -e
, it colorizes.
I checked manual page ofecho
, and-e
means enable interpretation of backslash escapes
How can I enable this option for PHP CLI? -
Barbayar Dashzeveg over 8 yearsyes, it worked after I replaced
\e
with033
. Thank you. -
jayxhj over 8 yearsand you can learn more here
-
4ae1e1 over 8 yearsOr do not rely on
echo
's sequences, and instead directly generate the ANSI sequences within the string using ANSI-C quoting:$'\e[31mblah blah\e[0m
. This way you don't need to remember\033
or whatever. -
İsmail Atkurt over 6 yearssingle quotes will not work by the way :) use double quotes
-
twknab almost 5 years+1 super helpful. FYI if anyone else needs help looking up color codes.
-
Brad over 3 yearsany way to get this to work when a PHP script is run via tty? Works from CLI normally but not in any git bash instances running in embedded terminals. (other commands show colours tho in the same setup)
-
supersan almost 3 yearsThis is a much better answer than the accepted answer
-
David Clews over 2 yearsthis would make a good helper function, also the time would be nice, $date = date('h:i:s', time());