Posted By

fobilow on 07/03/12


Tagged


Versions (?)

Coloured Terminal PHP


 / Published in: PHP
 

Code to support coloured output in php script run via a terminal

  1. # first define colors to use
  2. $_colors = array(
  3. "LIGHT_RED" => "[1;31m",
  4. "LIGHT_GREEN" => "[1;32m",
  5. "YELLOW" => "[1;33m",
  6. "LIGHT_BLUE" => "[1;34m",
  7. "MAGENTA" => "[1;35m",
  8. "LIGHT_CYAN" => "[1;36m",
  9. "WHITE" => "[1;37m",
  10. "NORMAL" => "[0m",
  11. "BLACK" => "[0;30m",
  12. "RED" => "[0;31m",
  13. "GREEN" => "[0;32m",
  14. "BROWN" => "[0;33m",
  15. "BLUE" => "[0;34m",
  16. "CYAN" => "[0;36m",
  17. "BOLD" => "[1m",
  18. "UNDERSCORE" => "[4m",
  19. "REVERSE" => "[7m",
  20.  
  21. );
  22. ##############################################
  23. # Output colorized text to terminal run
  24. # php scripts..
  25. ##############################################
  26. function termcolored($text, $color="NORMAL", $back=1){
  27. global $_colors;
  28. $out = $_colors["$color"];
  29. if($out == ""){ $out = "[0m"; }
  30. if($back){
  31. return chr(27)."$out$text".chr(27)."[0m";#.chr(27);
  32. }else{
  33. echo chr(27)."$out$text".chr(27).chr(27)."[0m";#.chr(27);
  34. }//fi
  35. }// end function
  36. ##############################################
  37.  
  38. echo termcolored("hello world","RED");

Report this snippet  

You need to login to post a comment.