Skip to content

Instantly share code, notes, and snippets.

@superbrothers
Created August 23, 2012 01:49
Show Gist options
  • Save superbrothers/3431198 to your computer and use it in GitHub Desktop.
Save superbrothers/3431198 to your computer and use it in GitHub Desktop.
ANSI color terminal output module for php
<?php
/**
* php-ansi-color
*
* Original
* https://github.com/loopj/commonjs-ansi-color
*
* @code
* <?php
* require_once "ansi-color.php";
*
* use PhpAnsiColor\Color;
*
* // Print the word "Error" to stdout in red
* error_log(Color::set("Error", "red"));
*
* // Print the word "Error" in red and underlined
* error_log(Color::set("Error", "red+underline"));
*
* // Print the word "Success" in bold green, followed by a message
* error_log(Color::set("Success", "green+bold"), "Something was successful!");
* @endcode
*/
namespace PhpAnsiColor;
class Color
{
protected static $ANSI_CODES = array(
"off" => 0,
"bold" => 1,
"italic" => 3,
"underline" => 4,
"blink" => 5,
"inverse" => 7,
"hidden" => 8,
"black" => 30,
"red" => 31,
"green" => 32,
"yellow" => 33,
"blue" => 34,
"magenta" => 35,
"cyan" => 36,
"white" => 37,
"black_bg" => 40,
"red_bg" => 41,
"green_bg" => 42,
"yellow_bg" => 43,
"blue_bg" => 44,
"magenta_bg" => 45,
"cyan_bg" => 46,
"white_bg" => 47
);
public static function set($str, $color)
{
$color_attrs = explode("+", $color);
$ansi_str = "";
foreach ($color_attrs as $attr) {
$ansi_str .= "\033[" . self::$ANSI_CODES[$attr] . "m";
}
$ansi_str .= $str . "\033[" . self::$ANSI_CODES["off"] . "m";
return $ansi_str;
}
public static function log($message, $color)
{
error_log(self::set($message, $color));
}
public static function replace($full_text, $search_regexp, $color)
{
$new_text = preg_replace_callback(
"/($search_regexp)/",
function ($matches) use ($color) {
return Color::set($matches[1], $color);
},
$full_text
);
return is_null($new_text) ? $full_text : $new_text;
}
}
@superbrothers
Copy link
Author

<?php
require_once "ansi-color.php";

use PhpAnsiColor\Color;

error_log(Color::set("Error", "red+underline"));
error_log(Color::set("Success", "green+bold") . " Something was successful!");

@prubini87
Copy link

Great, worked perfectly! Ty

@buckyball
Copy link

thx!

@nao360
Copy link

nao360 commented Jan 5, 2016

Exactly what I needed - thanks!

@morph027
Copy link

Thank you! ❤️

@MonicaAlcazar
Copy link

<3

@danny-source
Copy link

it's very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment