Harrington Web

How to use Colors in the Command Line output with PHP

Friday, December 13, 2013

Here is a simple function you can copy into your php file and it will format whatever section of text you place in it with a certain background and foreground color.

<?php
function colorFormatText($string, $textColor, $bgColor)
{
 return chr(27)."[".$textColor."m".chr(27)."[".$bgColor."m".$string.chr(27)."[0m";
}
?>

The Colors

Here is a list of the colors available using this method. There are only 8 colors.

Text  Background  Color
30  40          Black
31  41          Red
32  42          Green
33  43          Yellow
34  44          Dark Blue
35  45          Pink
36  46          Light Blue
37  47          White