Zend - The PHP Company




Miscellaneous

Add Code


Terminal color output in command line PHP  

Type: code fragment
Added by: joeldg
Entered: 14/10/2002
Last modified: 31/10/2001
Rating: - (fewer than 3 votes)
Views: 10420
At times it is useful to output colored terminal output when running command line PHP scripts. (i.e. Install scripts, general run scripts etc..).. Well, here is one way to do it that is pretty simple.


<?php
#####################################################################################
### termcolored() by Joel De Gan, joeldg 
### http://www.tenshimedia.com
### this function only useful for terminal run scripts.. I am not sure what this
### this would do run from a web based script.
### usage termcolored(text to output, color code);
### colors as follows for terminals..
###     default = 31(red)
###     1 boldish
###     4 = underline
###     7 = highlight
###     31 = red
###     32 = green
###     33 = yellow
###     34 = blue
###     35 = purple
###     36 = light blue
###     37 = pure white
###     41 - 47 reverse 31 - 37  so 41 is red background white text
#####################################################################################
function termcolored($text$color=31){
    echo 
exec("perl -e "print chr(27).'[01;$color m$text'.chr(27).'[00m';"");
}
// end function
?>


Usage Example




Rate This Script





Search



This Category All Categories