colorizing php cli scripts

It’s pretty common in most scripting languages which center around the command line (bash, perl, etc) to find information on colorizing your shell script output, mainly because those languages are tied very tightly to command line use. It can be difficult, however, to find information about adding this same nice feature to a php cli script. The reason is simple: most people dont use php for cli applications; most cli programmers use something else. It’s not difficult to adapt the same techniques listed in most bash howtos (generally in the section reserved for colorizing your command prompt) for generating colored terminal output for php.

echo "\033[31mred\033[37m\r\n";

echo "\033[32mgreen\033[37m\r\n";

echo "\033[41;30mblack on red\033[40;37m\r\n";

Simple, functional, useful (even if a bit complicated.) I leave it to you to lookup a bash prompt colorization howto to hunt down your own list of escape color codes (call it homework.)

Cheers

Comments (14)

  1. bamdad wrote::

    Hi

    i checked the code

    it doesn't work

    this is the output that php returns without any color :

    C:xampplitephp>php -f clicodestest.php

    ←[31mred←[37m

    ←[32mgreen←[37m

    ←[41;30mblack on red←[40;37m

    C:xampplitephp>

    Wednesday, December 19, 2007 at 2:51 AM #
  2. Bogdan wrote::

    Please see the MilcovLib library, it does just that: http://milcovlib.sf.net/

    Bamdad, it doesn't work under Windows, the original post specifically mentions bash.

    Saturday, April 19, 2008 at 5:34 AM #
  3. Wes wrote::

    Do you know if it's possible to assign the color codes to variables and then use them? I have played with it for a while, and no luck…

    Any clues?

    Saturday, May 10, 2008 at 12:42 AM #
  4. Thomas wrote::

    Here is a list of the codes, condensed into variables you can just place in your strings: (I have the same thing for my bash scripts)

    //Terminal Colour Codes (BASH CODES)

    $BLACK="33[0;30m";

    $DARKGRAY="33[1;30m";

    $BLUE="33[0;34m";

    $LIGHTBLUE="33[1;34m";

    $GREEN="33[0;32m";

    $LIGHTGREEN="33[1;32m";

    $CYAN="33[0;36m";

    $LIGHTCYAN="33[1;36m";

    $RED="33[0;31m";

    $LIGHTRED="33[1;31m";

    $PURPLE="33[0;35m";

    $LIGHTPURPLE="33[1;35m";

    $BROWN="33[0;33m";

    $YELLOW="33[1;33m";

    $LIGHTGRAY="33[0;37m";

    $WHITE="33[1;37m";

    Monday, September 22, 2008 at 2:43 AM #
  5. Thomas wrote::

    Ekkk, it left out some of the characters. There should be a backslash() and a zero (0) in front of each of those 33's.

    Monday, September 22, 2008 at 2:44 AM #
  6. Joar Wandborg wrote::

    Don't forget that you have to put double quotes around the text formatting string, if you dont do so, it will never be parsed as a "color".

    Tuesday, December 16, 2008 at 10:13 AM #
  7. peter_wall wrote::

    Thanks for this reference, always nice to find exactly what your looking for!

    *added to favourites!

    Pete

    Tuesday, October 6, 2009 at 11:01 PM #
  8. Roberto wrote::

    Wonderfull help. Thankyou very much.

    Best Regards

    Tuesday, December 1, 2009 at 10:58 PM #
  9. Pint James wrote::

    Hi

    Is there a code for returing to default color?

    Monday, July 12, 2010 at 12:15 AM #
  10. apokalyptik wrote::

    http://www.google.com/#q=bash+color+reset shows some things that you can do for resetting the colors.

    Monday, July 12, 2010 at 1:20 AM #
  11. Pint James wrote::

    Thanks

    Monday, July 12, 2010 at 5:13 AM #
  12. walter wrote::

    useful. just for linux, right?

    Wednesday, July 21, 2010 at 12:02 AM #
  13. العاب wrote::

    it's very nice also you can use str_pad to style your lines like linux in boot time

    like:

    loading http [done]

    thank you

    Saturday, June 4, 2011 at 12:26 PM #
  14. Rune Kaagaard wrote::

    Just wrote this: https://github.com/runekaagaard/php-termcolor

    Thursday, August 4, 2011 at 9:26 PM #