PHP CLI Colors – PHP Class Command Line Colors (bash) - Comment Page: 2

PHP Command Line Interface (CLI) has not built-in coloring for script output, like example Perl language has ( So I decided to make own class for adding colors on PHP CLI output. This class works only Bash shells. This class is easy to use. Just create new instance of class and call getColoredString function with string and foreground color and/or background color. [inttf_post_ad1] PHP Class for Coloring PHP Command Line (CLI) Scripts Output <?php class Colors { private $foreground_colors = array(); private $background_colors = array(); public function __construct() { // Set up shell...

37 comments on “PHP CLI Colors – PHP Class Command Line Colors (bash) - Comment Page: 2

1 2
    1. Nice job mate ;-)

    2. I would say like David, as it was a non-existent feature, your job is excellent; it honours the GNU spirit. Thanks

    3. This could easily be a static function for class-oriented programming

    4. Thanks for this, used it in my project :) I second the comment that this could be better static but you made my CLI output pretty

    5. This is really very useful function and programs provide….

    6. This is the first result I always find when searching for “PHP Console Color” but isn’t quite what I’m looking for. On linux, php 5.6 in order to enable color in the command line I always have to install the php-process module.

      Drives me insane every time. Hope this helps someone. :)

    7. I created this variation and I like it better:
      function strcolor($str,$fgcolor=”white”,$bgcolor=null)
      static $fgcolors = array(‘black’ => ‘0;30’,
      ‘dark gray’ => ‘1;30’,
      ‘blue’ => ‘0;34’,
      ‘light blue’ => ‘1;34’,
      ‘green’ => ‘0;32’,
      ‘light green’ => ‘1;32’,
      ‘cyan’ => ‘0;36’,
      ‘light cyan’ => ‘1;36’,
      ‘red’ => ‘0;31’,
      ‘light red’ => ‘1;31’,
      ‘purple’ => ‘0;35’,
      ‘light purple’ => ‘1;35’,
      ‘brown’ => ‘0;33’,
      ‘yellow’ => ‘1;33’,
      ‘light gray’ => ‘0;37’,
      ‘white’ => ‘1;37’);
      static $bgcolors = array(
      ‘black’ => ’40’,
      ‘red’ => ’41’,
      ‘green’ => ’42’,
      ‘yellow’ => ’43’,
      ‘blue’ => ’44’,
      ‘magenta’ => ’45’,
      ‘cyan’ => ’46’,
      ‘light gray’ => ’47’,);


      if (!isset($fgcolors[$fgcolor]))
      if (!isset($bgcolors[$bgcolor]))

      if ($fgcolor)
      $out .= “\033[{$fgcolors[$fgcolor]}m”;
      if ($bgcolor)
      $out .= “\033[{$bgcolors[$bgcolor]}m”;

      $out .= $str . “\033[0m”;

      return $out;

      • Yeah! It’s really better

    8. define(‘cli_reset’, “\033[0m”);
      define(‘cli_bold’, “\033[1m”);
      define(‘cli_clreol’, “\033[K”);

      define(‘cli_black’, “\033[30m”);
      define(‘cli_red’, “\033[31m”);
      define(‘cli_green’, “\033[32m”);
      define(‘cli_yellow’, “\033[33m”);
      define(‘cli_blue’, “\033[34m”);
      define(‘cli_magenta’, “\033[35m”);
      define(‘cli_cyan’, “\033[36m”);
      define(‘cli_white’, “\033[37m”);

      define(‘cli_black_bg’, “\033[40m”);
      define(‘cli_red_bg’, “\033[41m”);
      define(‘cli_green_bg’, “\033[42m”);
      define(‘cli_yellow_bg’, “\033[43m”);
      define(‘cli_blue_bg’, “\033[44m”);
      define(‘cli_magenta_bg’, “\033[45m”);
      define(‘cli_cyan_bg’, “\033[46m”);
      define(‘cli_white_bg’, “\033[47m”);

      define(‘cli_error’, “\033[41;30m” . cli_clreol);
      define(‘cli_warning’, “\033[43;30m” . cli_clreol);
      define(‘cli_info’, “\033[44;30m” . cli_clreol);
      define(‘cli_success’, “\033[42;30m” . cli_clreol);

      echo cli_error, “Error”, cli_reset;
      echo cli_red, cli_bold, “Bold red text”, cli_reset;

1 2

Leave a Reply

Your email address will not be published. Required fields are marked *