color.class.php

<?php
 
    class colors {
        private $foreground_colors = array();
        private $background_colors = array();
 
        public function __construct() {
            // set up shell colors
            $this->foreground_colors[‘black’] = ‘0;30’;
            $this->foreground_colors[‘dark_gray’] = ‘1;30’;
            $this->foreground_colors[‘blue’] = ‘0;34’;
            $this->foreground_colors[‘light_blue’] = ‘1;34’;
            $this->foreground_colors[‘green’] = ‘0;32’;
            $this->foreground_colors[‘light_green’] = ‘1;32’;
            $this->foreground_colors[‘cyan’] = ‘0;36’;
            $this->foreground_colors[‘light_cyan’] = ‘1;36’;
            $this->foreground_colors[‘red’] = ‘0;31’;
            $this->foreground_colors[‘light_red’] = ‘1;31’;
            $this->foreground_colors[‘purple’] = ‘0;35’;
            $this->foreground_colors[‘light_purple’] = ‘1;35’;
            $this->foreground_colors[‘brown’] = ‘0;33’;
            $this->foreground_colors[‘yellow’] = ‘1;33’;
            $this->foreground_colors[‘light_gray’] = ‘0;37’;
            $this->foreground_colors[‘white’] = ‘1;37’;
 
            $this->background_colors[‘black’] = ’40’;
            $this->background_colors[‘red’] = ’41’;
            $this->background_colors[‘green’] = ’42’;
            $this->background_colors[‘yellow’] = ’43’;
            $this->background_colors[‘blue’] = ’44’;
            $this->background_colors[‘magenta’] = ’45’;
            $this->background_colors[‘cyan’] = ’46’;
            $this->background_colors[‘light_gray’] = ’47’;
        }
 
        // returns colored string
        public function getcoloredstring($string, $foreground_color = null, $background_color = null) {
            $colored_string = “”;
 
            // check if given foreground color found
            if (isset($this->foreground_colors[$foreground_color])) {
                $colored_string .= “\033[” . $this->foreground_colors[$foreground_color] . “m”;
            }
            // check if given background color found
            if (isset($this->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;
        }
 
        // returns all foreground color names
        public function getforegroundcolors() {
            return array_keys($this->foreground_colors);
        }
 
        // returns all background color names
        public function getbackgroundcolors() {
            return array_keys($this->background_colors);
        }
    }
 
?>

 

 

banner.php

<?php
include(“color.class.php”);

$colors = new colors();

$a=$colors->getcoloredstring(”   ##        ###       “, “light_red”);

$x=$b=$colors->getcoloredstring(”     c0d3b3:”, “light_red”,”red”);

$b=$colors->getcoloredstring(“lost”, “light_red”,”red”);

$c=$colors->getcoloredstring(“wolf      “, “light_red”,”red”);
echo “\r\n”;

echo  ”                                                $x”.”$b”.”$c”;

echo “\n\r\n\r\n\r\n\r\n”;

echo  ”                        ########                    #”.”\n”;
echo  ”                    #################              #”.”\n”;
echo  ”                  ######################          #”.”\n”;
echo  ”                  #########################      #”.”\n”;
echo  ”                ############################”.”\n”;
echo  ”               ##############################”.”\n”;
echo  ”               ###############################”.”\n”;
echo  ”              ###############################”.”\n”;
echo  ”              ##############################”.”\n”;
echo  ”                              #    ########   #”;
echo  ”     \n               $a”;echo”####   ##”.”\n”;
echo  ”                                      ###   ###”.”\n”;
echo  ”                                    ####   ###”.”\n”;
echo  ”               ####          ##########   ####”.”\n”;
echo  ”               #######################   ####”.”\n”;
echo  ”                 ####################   ####”.”\n”;
echo  ”                  ##################  ####”.”\n”;
echo  ”                    ############      ##”.”\n”;
echo  ”                       ########        ###”.”\n”;
echo  ”                      #########        #####”.”\n”;
echo  ”                    ############      ######”.”\n”;
echo  ”                   ########      #########”.”\n”;
echo  ”                     #####       ########”.”\n”;
echo  ”                       ###       #########”.”\n”;
echo  ”                      ######    ############”.”\n”;
echo  ”                     #######################”.”\n”;
echo  ”                     #   #   ###  #   #   ##”.”\n”;
echo  ”                     ########################”.”\n”;
echo  ”                      ##     ##   ##     ##”.”\r\n\r\n”;

?>

colors class basic usage examples

<?php
 
    // create new colors class
    $colors = new colors();
 
    // test some basic printing with colors class
    echo $colors->getcoloredstring(“testing colors class, this is purple string on yellow background.”, “purple”, “yellow”) . “\n”;
    echo $colors->getcoloredstring(“testing colors class, this is blue string on light gray background.”, “blue”, “light_gray”) . “\n”;
    echo $colors->getcoloredstring(“testing colors class, this is red string on black background.”, “red”, “black”) . “\n”;
    echo $colors->getcoloredstring(“testing colors class, this is cyan string on green background.”, “cyan”, “green”) . “\n”;
    echo $colors->getcoloredstring(“testing colors class, this is cyan string on default background.”, “cyan”) . “\n”;
    echo $colors->getcoloredstring(“testing colors class, this is default string on cyan background.”, null, “cyan”) . “\n”;
 
?>

all foreground and background colors printed

 

 

 <?php
 
        // create new colors class
        $colors = new colors();
 
        // get foreground colors
        $fgs = $colors->getforegroundcolors();
        // get background colors
        $bgs = $colors->getbackgroundcolors();
 
        // loop through all foreground and background colors
        $count = count($fgs);
        for ($i = 0; $i < $count; $i++) {
            echo $colors->getcoloredstring(“test foreground colors”, $fgs[$i]) . “\t”;
            if (isset($bgs[$i])) {
                echo $colors->getcoloredstring(“test background colors”, null, $bgs[$i]);
            }
            echo “\n”;
        }
        echo “\n”;
 
        // loop through all foreground and background colors
        foreach ($fgs as $fg) {
            foreach ($bgs as $bg) {
                echo $colors->getcoloredstring(“test colors”, $fg, $bg) . “\t”;
            }
            echo “\n”;
        }
 
?>

//////////////////////////////////////////////////////////

ansi command line colors under windows

 

having colored text in the command line is a great help for spotting error or success messages. unfortunately, those of us developing under windows do not have this feature by default. here’s how to enable it:

  1. download ansicon from http://adoxa.110mb.com/ansicon/index.html
  2. extract the proper files (depending on if you have a 32 or 64 bit machine) to c:\ansicon\ (for example). i have a 32 bit machine and hence i extracted the files from inside the x86 folder.
  3. open a command line prompt and go to c:\ansicon, and then type “ansicon -i” without the quotes
  4. done

you can now enjoy the colored output of phpunit for example.

 

 

摘自 lostwolf’s blog