2

this is the issue.

When I use this php code echo "\033[31m some colored text \033[0m some white text \n"; to add some color to text I get [31m some colored text [0m some white text as response instead of colored text.

The CLI is actually showing colors, but not when I use php code.

How can I fix it?

I'm using Windows 10.

Thanks.

Screenshot of the CLI

  • you want to color text on an html page generated in php, isn't it? – kiks73 Dec 06 '17 at 19:10
  • No, I want to color text on command line, I'm generating a script and I want to add some color to responses. – Jesusvalopez Dec 06 '17 at 19:12
  • Have you tried to escape the \ chars? – kiks73 Dec 06 '17 at 19:13
  • Need to run `bash`. – AbraCadaver Dec 06 '17 at 19:17
  • @kiks73 It doesn't work. I've tried a lot of ways but it doesn't seems to recognize the colors. – Jesusvalopez Dec 06 '17 at 19:32
  • @AbraCadaver what do you mean with 'run bash'? I've used git bash and also windows cmd and the result was the same. – Jesusvalopez Dec 06 '17 at 19:34
  • https://stackoverflow.com/questions/7086034/colorizing-windows-command-line-output-from-php Seems to be a Windows "problem" and can be solved with external DLL/Modules – Felix Dec 06 '17 at 23:21
  • It's not explicitly a windows problem. I've run into the same issue on OSX. Color works fine from shell or php commands executed directly from shell, but the same color codes in PHP files fail and just show plaintext. – mopsyd Dec 11 '17 at 23:37

1 Answers1

0

PHP CLI output is aka plain text and the color depends on settings of the on terminal which is basically monochromatic (white on black, etc.).

The colors in your example \033[31m are color definitions from shell script expected to be running in linux-like terminal. Colored shell script output library

Windows command prompts supports colours however for batch scripts or so colors in the Windows command line

Edit: on your screenshot is PHP output a plaintext white on black as the terminal color schema is. The echo with red on black is output of shell echo command which is interpreted bash command and it has the power to colorize the output.

ino
  • 2,345
  • 1
  • 15
  • 27