PHP CLI & Terminal Colors

A Simple PHP class for wrapping your console/terminal output text in color. Keep in mind that building a class for such projects will require customization as each terminal emulator will process colors a bit differently. For the most part, the colors in this class work with any popular terminal emulator.

<?php

class CLI
{

// this function requires 1 variable, content. The color arguement is optional.
function cout_color($content, $color=null)
{

// if a color is set use the color set.
if(!empty($color))
{
// if our color string is not a numeric value
if(!is_numeric($color))
{
//lowercase our string value.
$c = strtolower($color);

}
else
{
// chec if our color value is not empty.
if(!empty($color))
{

$c = $color;

}
else
{ 
// no color was set so lets pick a random one...
$c = rand(1,14);

}

}

}
else// no color @paramter was passed, so lets pick a random one...
{ 

$c = rand(1,14);

}

$cheader = '';
$cfooter = "\033[0m";

// let check which color code was used so we can then wrap our content.
switch($c)
{

case 1:
case 'red':

// color code header.
$cheader .= "\033[31m";

break;

case 2:
case 'green':

// color code
$cheader .= "\033[32m";

break;

case 3:
case 'yellow':

// color code
$cheader .= "\033[33m";

break;

case 4:
case 'blue':

// color code
$cheader .= "\033[34m";

break;

case 5:
case 'magenta':

// color code
$cheader .= "\033[35m";

break;

case 6:
case 'cyan':

// color code
$cheader .= "\033[36m";

break;

case 7:
case 'light grey':

// color code
$cheader .= "\033[37m";

break;

case 8:
case 'dark grey':

// color code
$cheader .= "\033[90m";

break;

case 9:
case 'light red':

// color code
$cheader .= "\033[91m";

break;

case 10:
case 'light green':

// color code
$cheader .= "\033[92m";

break;

case 11:
case 'light yellow':

// color code
$cheader .= "\033[93m";

break;

case 12:
case 'light blue':

// color code
$cheader .= "\033[94m";

break;

case 13:
case 'light magenta':

// color code
$cheader .= "\033[95m";

break;

case 14:
case 'light cyan':

// color code
$cheader .= "\033[92m";

break;

}

// wrap our content.
$content = $cheader.$content.$cfooter;

//return our new content.
return $content;


}



}

?>


$cli = new CLI();

// echo a string using the class string color 'red'.
echo $cli->cout_color('It Works!', 'red');

// echo a string using the class color id 1.
echo $cli->cout_color('It Works!', 1);

// echo a string using a random color.
echo $cli->cout_color('It Works!');

Add/Remove based on your project requirements.

Published by

Miguel

I’m a bilingual Network Engineer. I have over 20 Years of Professional experience in Computer Science & Information Technology. I currently own and operate Web Semantics (www.websemantics.com) in Vancouver, Washington. I provide bilingual (English & Spanish) enterprise-level IT support to small and medium-sized businesses across the West Coast. *** Soy un ingeniero de redes bilingüe. Tengo más de 20 años de experiencia profesional en ciencias de la computación y tecnología de la información. Actualmente poseo y opero Web Semantics (www.websemantics.com) en Vancouver, Washington. Proporciono soporte de IT/Informática bilingüe (inglés y español) a nivel empresarial a pequeñas y medianas empresas en toda la costa oeste.

Leave a Reply