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

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: 3

1 2 3
    1. Great script!

      I prefer this style more though:

      “`
      class CLIString {

      public $colors = 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’,
      );
      public $backgroundColors = array(
      ‘black’ => ’40’,
      ‘red’ => ’41’,
      ‘green’ => ’42’,
      ‘yellow’ => ’43’,
      ‘blue’ => ’44’,
      ‘magenta’ => ’45’,
      ‘cyan’ => ’46’,
      ‘light_gray’ => ’47’,
      );

      public $string = null;
      public $color = null;
      public $background = null;
      public $newLine = false;

      public function string($string)
      {
      $this->string = (string) $string;
      return $this;
      }

      public function color($string)
      {
      $this->color = (string) $string;
      return $this;
      }

      public function background($string)
      {
      $this->background = (string) $string;
      return $this;
      }

      public function newLine()
      {
      $this->newLine = true;
      return $this;
      }

      // Returns colored string
      public function get()
      {
      $string = ”;

      // Check if given foreground color found
      if (isset($this->colors[$this->color])) {
      $string .= “\033[” . $this->colors[$this->color] . “m”;
      }
      // Check if given background color found
      if (isset($this->backgroundColors[$this->background])) {
      $string .= “\033[” . $this->backgroundColors[$this->background] . “m”;
      }

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

      if ($this->newLine) {
      $string .= “\n”;
      }

      return $string;
      }

      // Returns all foreground color names
      public function getForegroundColors() {
      return array_keys($this->colors);
      }

      // Returns all background color names
      public function getBackgroundColors() {
      return array_keys($this->backgroundColors);
      }
      }

      echo (new CLIString)
      ->string(‘Hey there’)
      ->background(‘black’)
      ->color(‘cyan’)
      ->newLine()
      ->get();
      “`

      Reply
    2. Some terminals support true color allowing full RGB.

      function rgb($str, $r, $g, $b) {
      if (
      !is_numeric($r) ||
      !is_numeric($g) ||
      !is_numeric($b) ||
      $r 255 ||
      $g 255 ||
      $b 255
      ) {
      throw new Exception(‘rgb: Invalid function parameters!’, -1);
      }

      return “x1b[38;2;$r;$g;{$b}m$strx1b[0m”;
      }

      Reply
      • The commenting system removed the formatting and some characters from my post. Not sure how or if it’s possible to comment code here, maybe with backticks? “`$r 255“`
        Anyway, the conditions are supposed to check if $r, $g, and $b are less than zero or greater than 255 (valid rgb range).

        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