Oct 252008
 

If you are doing any PHP command line scripting, you may have wondered how you can add a bit of simple colouring to your scripts so it is easier for you, or your clients to view the output.

Using some Bash codes, you can colour the output to the terminal (assuming you are running the scripts in Bash or some other compatible terminal). Depending on the font colours and effects you want, you can use any of this code in your script. Just place it at the top, and include the variables in your output:


/*
 * Font Colours
 */
$BLACK="33[0;30m"; //Can not be used on backgrounds
$DARKGRAY="33[1;30m"; //Can be used on backgrounds
$RED="33[0;31m"; //Can not be used on backgrounds
$LIGHTRED="33[1;31m"; //Can be used on backgrounds
$GREEN="33[0;32m"; //Can not be used on backgrounds
$LIGHTGREEN="33[1;32m"; //Can be used on backgrounds
$BROWN="33[0;33m"; //Can not be used on backgrounds
$YELLOW="33[1;33m"; //Can be used on backgrounds
$BLUE="33[0;34m"; //Can not be used on backgrounds
$LIGHTBLUE="33[1;34m"; //Can be used on backgrounds
$PURPLE="33[0;35m"; //Can not be used on backgrounds
$LIGHTPURPLE="33[1;35m"; //Can be used on backgrounds
$CYAN="33[0;36m"; //Can not be used on backgrounds
$LIGHTCYAN="33[1;36m"; //Can be used on backgrounds
$LIGHTGRAY="33[0;37m"; //Can not be used on backgrounds
$WHITE="33[1;37m"; //Can be used on backgrounds
/*
 * Bolded colours that can be used on backgrounds
 * Duplicates of the colours above with BOLD in the name
 */
$BOLDBLACK="33[1;30m";
$BOLDRED="33[1;31m";
$BOLDGREEN="33[1;32m";
$BOLDBROWN="33[1;33m";
$BOLDBLUE="33[1;34m";
$BOLDPURPLE="33[1;35m";
$BOLDCYAN="33[1;36m";
$BOLDGRAY="33[1;37m";
/*
 * Background Colours
 */
$BLACKBG="33[0;40m";
$REDBG="33[0;41m";
$GREENBG="33[0;42m";
$BROWNBG="33[0;43m";
$BLUEBG="33[0;44m";
$PURPLEBG="33[0;45m";
$CYANBG="33[0;46m";
$LIGHTGRAYBG="33[0;47m";
/*
 * Font Effects
 */
$UNDERLINE="33[4;30m";
$BLINK="33[5;30m"; //Doesn't seem to work.
$INVERSE="33[7;30m";
$INVISIBLE="33[8;30m"; //Pretty pointless
/*
 * Turn it back to the default
 */
$DEFAULT="33[0m";

To test this out, you can place this in a php script and execute it:


echo "<-- Font Colours -->
";
echo "$BLACK This is BLACK
";
echo "$DARKGRAY This is DARKGRAY
";
echo "$RED This is RED
";
echo "$LIGHTRED This is LIGHTRED
";
echo "$GREEN This is GREEN
";
echo "$LIGHTGREEN This is LIGHTGREEN
";
echo "$BROWN This is BROWN
";
echo "$YELLOW This is YELLOW
";
echo "$BLUE This is BLUE
";
echo "$LIGHTBLUE This is LIGHTBLUE
";
echo "$PURPLE This is PURPLE
";
echo "$LIGHTPURPLE This is LIGHTPURPLE
";
echo "$CYAN This is CYAN
";
echo "$LIGHTCYAN This is LIGHTCYAN
";
echo "$LIGHTGRAY This is LIGHTGRAY
";
echo "$WHITE This is WHITE
";

echo "<-- Backgrounds -->
";
echo "$BLACKBG This is BLACKBG
";
echo "$REDBG This is REDBG
";
echo "$GREENBG This is GREENBG
";
echo "$BROWNBG This is BROWNBG
";
echo "$BLUEBG This is BLUEBG
";
echo "$PURPLEBG This is PURPLEBG
";
echo "$CYANBG This is CYANBG
";
echo "$LIGHTGRAYBG This is LIGHTGRAYBG
";

echo "<-- Font Effects -->
";
echo "$UNDERLINE This is UNDERLINE{$DEFAULT}
";
echo "$BLINK This is BLINK //Doesn't seem to work.
";
echo "$INVERSE This is INVERSE{$DEFAULT}
";
echo "$INVISIBLE This is INVISIBLE {$DEFAULT}//Invisible - Pretty pointless
";
echo "$DEFAULT Back to the default.
";

echo "<-- A Few Examples -->
";
echo "{$REDBG}{$YELLOW}This is a red background, with yellow font that {$UNDERLINE}{$YELLOW}is underlined{$DEFAULT}{$REDBG}{$YELLOW} for a portion. {$DEFAULT}
";
echo "{$CYANBG}{$LIGHTRED}This is red text on a cyan background. {$DEFAULT}
";

And practical example would be:


//Check to see if courses.txt exists
if(!file_exists($courses)){
	echo "$RED The courses.txt file doesn't exist. $DEFAULT
";
	exit();
}

 Leave a Reply

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>

(required)

(required)