If Not True Then False

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...
Categories:
Tagged with:

37 Comments

Leave a Comment

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

Input your comment.
help

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Input your name.

Stéphane

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

reply Reply
phpuser

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 Reply
Dustin

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 Reply
AbiusX

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 Reply
Andrey Izman

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 Reply