note 89488 modified in function.chr by danbrown

From: Date: Tue, 10 Mar 2009 19:33:43 +0000
Subject: note 89488 modified in function.chr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-151698@lists.php.net to get a copy of this message
Thank you joeldegan for your termcolored function. I write a lot of command-line scripts using PHP and like to colorize them. I was having a problem with the termcolored function where the next character echo'ed after calling termcolored() was being dropped. Turns out you don't need the extra chr(27) at the end of each term color code. Here's my modified version of your function, renamed to echocolor. <?php function echocolor($text,$color="normal",$back=0) { $colors = array('light_red' => "[1;31m", 'light_green' => "[1;32m", 'yellow' => "[1;33m", 'light_blue' => "[1;34m", 'magenta' => "[1;35m", 'light_cyan' => "[1;36m", 'white' => "[1;37m", 'normal' => "[0m", 'black' => "[0;30m", 'red' => "[0;31m", 'green' => "[0;32m", 'brown' => "[0;33m", 'blue' => "[0;34m", 'cyan' => "[0;36m", 'bold' => "[1m", 'underscore' => "[4m", 'reverse' => "[7m" ); $out = $colors["$color"]; $ech = chr(27)."$out"."$text".chr(27)."[0m"; if($back) { return $ech; } else { echo $ech; } } ?> --was-- Thank you joeldegan for your termcolored function. I write a lot of command-line scripts using PHP and like to colorize them. I was having a problem with the termcolored function where the next character echo'ed after calling termcolored() was being dropped. Turns out you don't need the extra chr(27) at the end of each term color code. Here's my modified version of your function, renamed to echocolor. <? function echocolor($text,$color="normal",$back=0) { $colors = array('light_red' => "[1;31m", 'light_green' => "[1;32m", 'yellow' => "[1;33m", 'light_blue' => "[1;34m", 'magenta' => "[1;35m", 'light_cyan' => "[1;36m", 'white' => "[1;37m", 'normal' => "[0m", 'black' => "[0;30m", 'red' => "[0;31m", 'green' => "[0;32m", 'brown' => "[0;33m", 'blue' => "[0;34m", 'cyan' => "[0;36m", 'bold' => "[1m", 'underscore' => "[4m", 'reverse' => "[7m" ); $out = $colors["$color"]; $ech = chr(27)."$out"."$text".chr(27)."[0m"; if($back) { return $ech; } else { echo $ech; } } ?> http://php.net/manual/en/function.chr.php

« previous php.notes (#151698) next »