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 (perldoc.perl.org/Term/ANSIColor.html). 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...

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

1 2 3
    1. Nice job mate ;-)

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

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

      Reply
    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

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

      Reply
    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. :)

      Reply
    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’,);

      $out=””;

      if (!isset($fgcolors[$fgcolor]))
      $fgcolor=’white’;
      if (!isset($bgcolors[$bgcolor]))
      $bgcolor=null;

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

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

      return $out;
      }

      Reply
      • Yeah! It’s really better

        Reply
    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);

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

      Reply
    9. // A faster version

      // Returns colored string
      function getColoredString($string, $foreground_color = null, $background_color = null)
      {
      $foreground_colors = [
      ‘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’,];
      $background_colors = [
      ‘black’ => ’40’,
      ‘red’ => ’41’,
      ‘green’ => ’42’,
      ‘yellow’ => ’43’,
      ‘blue’ => ’44’,
      ‘magenta’ => ’45’,
      ‘cyan’ => ’46’,
      ‘light_gray’ => ’47’];

      $colored_string = “”;

      // Check if given foreground color found
      if (isset($foreground_colors[$foreground_color])) {
      $colored_string .= “\033[” . $this->foreground_colors[$foreground_color] . “m”;
      }
      // Check if given background color found
      if (isset($background_colors[$background_color])) {
      $colored_string .= “\033[” . $this->background_colors[$background_color] . “m”;
      }

      // Add string and end coloring
      $colored_string .= $string . “\033[0m”;

      return $colored_string;
      }

      Reply
    10. Hi, thank you so much, this is great and exactly what is needed, If your intrested If your intrested in console developed apps please send me an email, i will send my scripts, they are very unique, offer tasks management from console. Thank you again, very greatfull that you made this, just so perfect… :) thankyou thankyou thankyou :)

      Reply
    11. Hi, thank you so much, this is great and exactly what is needed

      Reply
    12. Is there any testing, whether the used shell supports colors?

      Reply
1 2 3

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close