0

I am encountering a bug where ANSI colors refuse to display when including a file.

The following bash command works as expected (straight bash):

echo -e "\033[31m some colored text \033[0m some white text" # Bash command displays fine

The following command run from the command line also works fine (php interpreter, command direct input via cli):

php -r 'echo "\033[31m some colored text \033[0m some white text \n";' # PHP from console displays fine

Including a file does not work.

Contents of test file:

<?php
//fails
$test = '"\033[31m some colored text \033[0m some white text \n"';
echo $test;
//escaped backslashes, also fails
$test = '"\\033[31m some colored text \\033[0m some white text \n"';
echo $test;

Including the file like this from the command line:

php index.php

Or including it like this from the command line:

php -f index.php

Always produces this:

"\033[31m some colored text \033[0m some white text \n""\033[31m some colored text \033[0m some white text \n"

Neither the line breaks nor the console colors display properly. What is causing this to choke specifically when a file runs as opposed to entering the command directly, either from bash or from PHP on the command line?


Env: I am running PHP 7.0 in iTerm on OSX, but testing in the native terminal and zsh also fails.

Screencap pictured for visualization purposes


I have already researched this issue and ruled out all of the following


Edit:

As some comments had pointed out, the above is double quoted. Single quoted also does not work, though other users indicate that it does work for them, which leads me to believe this is some kind of arcane misconfiguration issue and not a strict PHP language problem. Updated source as requested:

<?php
//fails
$test = "\033[31m some colored text \033[0m some white text \n";
echo $test;
//escaped backslashes, also fails
$test = "\\033[31m some colored text \\033[0m some white text \n";
echo $test;

Produces the same results anyways:

Revised screencap

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
mopsyd
  • 1,877
  • 3
  • 20
  • 30
  • Wait, you're not comparing like for like. In the command-line version, you have a **double**-quoted string; In the index.php version, you have a **single**-quoted string. – Oliver Charlesworth Dec 12 '17 at 00:22
  • The backslash character does not work in single quote strings. – Reactgular Dec 12 '17 at 00:22
  • It fails in double quoted strings also – mopsyd Dec 12 '17 at 00:22
  • But you put your double quotes inside a single quote! – Reactgular Dec 12 '17 at 00:23
  • Not for me :/ ... – Oliver Charlesworth Dec 12 '17 at 00:23
  • Running it like this: `$test = "\033[31m some colored text \033[0m some white text \n";` produces exactly the same results. – mopsyd Dec 12 '17 at 00:25
  • I expect it's some kind of strange config issue if it is working for other people. – mopsyd Dec 12 '17 at 00:26
  • Not here. Can you at least update your question (including the screenshot) so that you have the quoting correct. As it stands, the answer to the posted question is "use double quotes" :/ – Oliver Charlesworth Dec 12 '17 at 00:26
  • Sure thing, one sec. – mopsyd Dec 12 '17 at 00:27
  • 1
    Yeah, there's something truly bizarre going on - you're even seeing the literal `\n`s in your output. So not limited to ANSI escape sequences. – Oliver Charlesworth Dec 12 '17 at 00:46
  • I hadn't noticed the `\n` thanks for pointing that out. Something is apparently getting in between the script and the command line output and escaping everything, but that gives me a few ideas what it might be. I'll update after I poke around a bit. – mopsyd Dec 12 '17 at 00:49
  • Try `\e[31m` instead of .`\033[31m` - or use a lib like [CliMate](https://github.com/thephpleague/climate) to save a whole lot of pain and ugly strings. – Lawrence Cherone Dec 12 '17 at 00:53
  • They are doing both. First `php -f index.php` to show results, then `cat index.php` is to display the file contents after the command fails. – mopsyd Dec 12 '17 at 00:59
  • I just ran the exact same thing in a Mint VM and it worked as expected. There's some strange config issue going on, possibly a docker issue or a homebrew issue in my main dev env. – mopsyd Dec 12 '17 at 01:00

0 Answers0