Skip to content

Instantly share code, notes, and snippets.

@leihog
Created June 28, 2012 13:38
Show Gist options
  • Save leihog/3011434 to your computer and use it in GitHub Desktop.
Save leihog/3011434 to your computer and use it in GitHub Desktop.
colorize function in PHP
<?php
function colorize($str, $fg = 'white', $bg = false)
{
$fg = str_replace(array('-','_'), '', $fg);
switch($fg) {
case 'black': $fg = "0;30"; break;
case 'yellow': $fg = "1;33"; break;
case 'brown': $fg = "0;33"; break;
case 'green': $fg = "0;32"; break;
case 'lightgreen': $fg = "1;32"; break;
case 'blue': $fg = "0;34"; break;
case 'lightblue': $fg = "1;34"; break;
case 'dark_grey': $fg = "1;30"; break;
case 'lightgrey': $fg = "0;37"; break;
case 'cyan': $fg = "0;36"; break;
case 'lightcyan': $fg = "1;36"; break;
case 'red': $fg = "0;31"; break;
case 'lightred': $fg = "1;31"; break;
case 'purple': $fg = "0;35"; break;
case 'lightpurple': $fg = "1;35"; break;
case 'white':
default:
$fg = "1;37";
}
switch($bg) {
case 'black': $bg = "40"; break;
case 'red': $bg = "41"; break;
case 'green': $bg = "42"; break;
case 'yellow': $bg = "43"; break;
case 'blue': $bg = "44"; break;
case 'magenta': $bg = "45"; break;
case 'cyan': $bg = "46"; break;
case 'light_grey': $bg = "47"; break;
default:
}
return "\033[{$fg}m".
($bg ? "\033[{$bg}m" : '').
$str ."\033[0m\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment