6

Suppose I've a php file and I want to change text-colour of my output for a console based application...

<?php
$prompt = "What Is Your Name: ";
echo $prompt;
$answer =  "You Entered: " . rtrim( fgets( STDIN ));
echo $answer;
?>

I want to change text-colour of $answer.

Is it possible? If yes, how can I do this?

sotirov
  • 4,379

3 Answers3

7

This is certainly possible using ANSI escape codes:

<?php
echo "\033[31m some colored text \033[0m some white text \n";
?>

will output "some colored text" in red and "some white text" in white (unless you chose different default colours for your terminal).

The characters \033 indicate the start of an escape code. [31m is the colour red. [0m signifies the end of the coded fragment. For more colours, see here.

For other effects, such as bold:

\033[1m bold text \033[0m

see the first link. You can try out these codes on a command line by doing thing like:

echo -e "\033[1m bold text \033[0m"
Jos
  • 30,529
  • 8
  • 89
  • 96
3

Now you can use 24-bit true color in terminal in Ubuntu 16.04

enter image description here

  • The foreground escape sequence is ^[38;2;<red>;<green>;<blue>m
  • The background escape sequence is ^[48;2;<red>;<green>;<blue>m
  • <red> <green> <blue> range from 0 to 255 inclusive.
  • The escape sequence ^[0m returns output to default.

See RGB Color Codes Chart

Demonstration of 24-bit true color in a script:

enter image description here

Here is the modified script to produce colored output.

<?php
$prompt = "What Is Your Name: ";
echo $prompt;
$answer =  rtrim( fgets( STDIN ));
echo "\033[38;2;0;102;0m You \033[38;2;255;0;255m Entered: \033[38;2;255;255;0m $answer \033[0m \n";
?>

Sample Output of the above script:

Sample output

sourav c.
  • 46,120
3

Instead of hard-coded sequences, you should use a library such as PHP Ncurses which will be more likely to work on more terminal types

The example from ncurses_color_set:

<?php
ncurses_init();

// If the terminal supports colors, initialize and set active color
if (ncurses_has_colors()) {
    ncurses_start_color();
    ncurses_init_pair(1, NCURSES_COLOR_YELLOW, NCURSES_COLOR_BLUE);
    ncurses_color_set(1);
}

// Write a string at specified location
ncurses_mvaddstr(10, 10, "Hello world! Yellow on blue text!");

// Flush output to screen
ncurses_refresh();

ncurses_end();
?>

From ncurses_has_colors:

Checks whether the terminal has color capabilities. This function can be used to write terminal-independent programs.

[emphasis mine]

Use ncurses_attr(NCURSES_A_BOLD); for bold. Note that this function and the related ones are marked as experimental.

From ncurses_attron

Warning This function is EXPERIMENTAL. The behaviour of this function, its name, and surrounding documentation may change without notice in a future release of PHP. This function should be used at your own risk.

Warning This function is currently not documented; only its argument list is available.

[emphasis theirs]

You may find other libraries. You should check to make sure they use terminal-independent techniques. I have found several that use hard-coded sequences instead.

Note that the command-line (shell) equivalent uses tput in order to be terminal independent. I include this information as a starting point for reference. They should be avoided.